Learning GO
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
scan := bufio.NewScanner(os.Stdin)
|
||||||
|
words := make(map[string]int)
|
||||||
|
scan.Split(bufio.ScanWords)
|
||||||
|
|
||||||
|
total := 0
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
words[scan.Text()]++
|
||||||
|
total++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("this file has about %v words and ", total)
|
||||||
|
fmt.Println(len(words), "Unique words")
|
||||||
|
|
||||||
|
type kv struct {
|
||||||
|
key string
|
||||||
|
val int
|
||||||
|
}
|
||||||
|
|
||||||
|
var ss []kv
|
||||||
|
for k, v := range words {
|
||||||
|
ss = append(ss, kv{k, v})
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(ss, func(i, j int) bool {
|
||||||
|
return ss[i].val > ss[j].val
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, s := range ss[:4] {
|
||||||
|
fmt.Println(s.key, "appears", s.val, "times")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
go 1.26.5
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Hello there, Bright morning hikers crossed quiet valleys while curious birds circled overhead. Each traveler carried water, maps, snacks, sturdy boots, and cameras. They discovered ancient stones, fragrant flowers, distant waterfalls, colorful butterflies, mossy bridges, peaceful ponds by sunset. Everyone shared stories, laughter, gratitude, and plans another adventure. Bright morning hikers crossed valleys, while curious birds circled overhead, and everyone shared stories.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module main.go
|
||||||
|
|
||||||
|
go 1.23.1
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func condition() {
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
if i%2 == 0 {
|
||||||
|
fmt.Println("this number is even: ", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sqrt(x float64) float64 {
|
||||||
|
z := 1.0
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
z -= (z*z - x) / (z * 2)
|
||||||
|
fmt.Println(z)
|
||||||
|
}
|
||||||
|
return z
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer condition()
|
||||||
|
fmt.Println(sqrt(2))
|
||||||
|
|
||||||
|
fmt.Print("Go runs on ")
|
||||||
|
switch os := runtime.GOOS; os {
|
||||||
|
case "Windows":
|
||||||
|
fmt.Println("MacOs.")
|
||||||
|
case "Linx":
|
||||||
|
fmt.Println("Linux.")
|
||||||
|
default:
|
||||||
|
fmt.Printf("%s.\n", os)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("*******************")
|
||||||
|
fmt.Println("When's Saturday?")
|
||||||
|
today := time.Now().Weekday()
|
||||||
|
switch time.Saturday {
|
||||||
|
case today + 0:
|
||||||
|
fmt.Println("Today.")
|
||||||
|
case today + 1:
|
||||||
|
fmt.Println("Tomorrow.")
|
||||||
|
case today + 2:
|
||||||
|
fmt.Println("In two days.")
|
||||||
|
default:
|
||||||
|
fmt.Println("Too far away.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const name = "Sara"
|
||||||
|
|
||||||
|
love := "Kahlousha"
|
||||||
|
number := 43900900
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
const world = "Mykids"
|
||||||
|
fmt.Println("Hello,", world)
|
||||||
|
|
||||||
|
fmt.Println("I love ", name)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module hello
|
||||||
|
|
||||||
|
go 1.23.1
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Create a huge number by shifting a 1 bit left 100 places.
|
||||||
|
// In other words, the binary number that is 1 followed by 100 zeroes.
|
||||||
|
Big = 1 << 100
|
||||||
|
|
||||||
|
//shoft it right 99 places
|
||||||
|
Small = Big >> 99
|
||||||
|
)
|
||||||
|
|
||||||
|
func needInt(x int) int {
|
||||||
|
return x*10 + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func needFloat(y float64) float64 { return y * 0.1 }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(Small)
|
||||||
|
fmt.Println(needInt(Small))
|
||||||
|
fmt.Println(needFloat(Small))
|
||||||
|
fmt.Println(needFloat(Big))
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
go 1.26.5
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
15
|
||||||
|
22
|
||||||
|
34
|
||||||
|
34
|
||||||
|
64
|
||||||
|
53
|
||||||
|
55
|
||||||
|
9
|
||||||
|
87
|
||||||
|
6
|
||||||
|
74
|
||||||
|
346
|
||||||
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
var sum float64
|
||||||
|
var n int
|
||||||
|
|
||||||
|
for {
|
||||||
|
var val float64
|
||||||
|
if _, err := fmt.Fscanln(os.Stdin, &val); err != nil {
|
||||||
|
fmt.Println("Something went wrong!")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
sum += val
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
fmt.Fprintln(os.Stderr, "no Values")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
average := sum / float64(n)
|
||||||
|
fmt.Println("The average of the total numbers is:", average)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
go 1.26.5
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
There was a kid named Sami, and his sister Leena.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
There was a kid named Sami, and his sister Girl.
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// s := "élite"
|
||||||
|
//
|
||||||
|
// fmt.Printf("%8T %[1]v %d\n", s, len(s))
|
||||||
|
// r := []rune(s)
|
||||||
|
// fmt.Printf("%8T %[1]v %d\n", r, len(r))
|
||||||
|
// b := []byte(s)
|
||||||
|
// fmt.Printf("%8T %[1]v %d\n", b, len(b))
|
||||||
|
|
||||||
|
// s := "The Quick brown fox"
|
||||||
|
//
|
||||||
|
// length := len(s)
|
||||||
|
//
|
||||||
|
// b := s[:3]
|
||||||
|
// c := s[3:9]
|
||||||
|
// d := s[:4] + "Fast" + s[9:]
|
||||||
|
//
|
||||||
|
// s += "es"
|
||||||
|
//
|
||||||
|
// fmt.Println(length)
|
||||||
|
// fmt.Println(b)
|
||||||
|
// fmt.Println(c)
|
||||||
|
// fmt.Println(d)
|
||||||
|
// fmt.Println(s)
|
||||||
|
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Fprintln(os.Stderr, "Not enough args")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
old, replacemnt := os.Args[1], os.Args[2]
|
||||||
|
scan := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
s := strings.Split(scan.Text(), old)
|
||||||
|
t := strings.Join(s, replacemnt)
|
||||||
|
|
||||||
|
fmt.Println(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"hello"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(hello.Say(os.Args[1:]))
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module hello
|
||||||
|
|
||||||
|
go 1.26.5
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package hello
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Say(names []string) string {
|
||||||
|
|
||||||
|
if len(names) == 0 {
|
||||||
|
names = []string{"world"}
|
||||||
|
}
|
||||||
|
return "Hello, " + strings.Join(names, ", ") + "!"
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package hello
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSayHello(t *testing.T) {
|
||||||
|
|
||||||
|
subtests := []struct {
|
||||||
|
items []string
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
result: "Hello, world!",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
items: []string{"Leena"},
|
||||||
|
result: "Hello, Leena!",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
items: []string{"Zack", "Sami", "Sara"},
|
||||||
|
result: "Hello, Zack, Sami, Sara!",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, st := range subtests {
|
||||||
|
if s := Say(st.items); s != st.result {
|
||||||
|
t.Errorf("wanted %s (%v), got %s", st.result, st.items, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user