commit d307eb2698413fa4987e63f49f2ef7564878f380 Author: Zakaria Date: Mon Aug 31 14:57:35 2026 -0400 Learning GO diff --git a/Composite_Types/composite_types.go b/Composite_Types/composite_types.go new file mode 100644 index 0000000..0d74ffb --- /dev/null +++ b/Composite_Types/composite_types.go @@ -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") + } +} diff --git a/Composite_Types/go.mod b/Composite_Types/go.mod new file mode 100644 index 0000000..a9eb3f6 --- /dev/null +++ b/Composite_Types/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.26.5 diff --git a/Composite_Types/test.txt b/Composite_Types/test.txt new file mode 100644 index 0000000..e86c306 --- /dev/null +++ b/Composite_Types/test.txt @@ -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. diff --git a/For_If_Switch/go.mod b/For_If_Switch/go.mod new file mode 100644 index 0000000..96dbddb --- /dev/null +++ b/For_If_Switch/go.mod @@ -0,0 +1,3 @@ +module main.go + +go 1.23.1 diff --git a/For_If_Switch/main.go b/For_If_Switch/main.go new file mode 100644 index 0000000..2101c78 --- /dev/null +++ b/For_If_Switch/main.go @@ -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.") + } + +} diff --git a/courses/1 b/courses/1 new file mode 100644 index 0000000..50c6344 --- /dev/null +++ b/courses/1 @@ -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) + +} diff --git a/courses/go.mod b/courses/go.mod new file mode 100644 index 0000000..378a044 --- /dev/null +++ b/courses/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.23.1 diff --git a/courses/main.go b/courses/main.go new file mode 100644 index 0000000..b3719a9 --- /dev/null +++ b/courses/main.go @@ -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)) +} diff --git a/pointers/go.mod b/pointers/go.mod new file mode 100644 index 0000000..a9eb3f6 --- /dev/null +++ b/pointers/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.26.5 diff --git a/pointers/nums.txt b/pointers/nums.txt new file mode 100644 index 0000000..70b3a17 --- /dev/null +++ b/pointers/nums.txt @@ -0,0 +1,13 @@ +15 +22 +34 +34 +64 +53 +55 +9 +87 +6 +74 +346 + diff --git a/pointers/pointers.go b/pointers/pointers.go new file mode 100644 index 0000000..dfaeac1 --- /dev/null +++ b/pointers/pointers.go @@ -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) + +} diff --git a/strings/go.mod b/strings/go.mod new file mode 100644 index 0000000..a9eb3f6 --- /dev/null +++ b/strings/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.26.5 diff --git a/strings/lineofwords.txt b/strings/lineofwords.txt new file mode 100644 index 0000000..e8b79da --- /dev/null +++ b/strings/lineofwords.txt @@ -0,0 +1 @@ +There was a kid named Sami, and his sister Leena. diff --git a/strings/postProg.txt b/strings/postProg.txt new file mode 100644 index 0000000..2054f1c --- /dev/null +++ b/strings/postProg.txt @@ -0,0 +1 @@ +There was a kid named Sami, and his sister Girl. diff --git a/strings/string_course.go b/strings/string_course.go new file mode 100644 index 0000000..59f35ff --- /dev/null +++ b/strings/string_course.go @@ -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) + } +} diff --git a/types/cmd/main.go b/types/cmd/main.go new file mode 100644 index 0000000..a14c74b --- /dev/null +++ b/types/cmd/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "hello" + "os" +) + +func main() { + fmt.Println(hello.Say(os.Args[1:])) +} diff --git a/types/go.mod b/types/go.mod new file mode 100644 index 0000000..96b3692 --- /dev/null +++ b/types/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.26.5 diff --git a/types/hello.go b/types/hello.go new file mode 100644 index 0000000..20b0265 --- /dev/null +++ b/types/hello.go @@ -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, ", ") + "!" +} diff --git a/types/hello_test.go b/types/hello_test.go new file mode 100644 index 0000000..f7abc37 --- /dev/null +++ b/types/hello_test.go @@ -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) + } + } +}