From cfd7ab8a0ba1a91d56ff8432895c03e98d680750 Mon Sep 17 00:00:00 2001 From: V Date: Sat, 30 Aug 2025 19:14:01 +0100 Subject: [PATCH] Sync --- tour_of_go/l4/2 | 39 --------------------- tour_of_go/l4/exercise-fibonacci-closure.go | 28 +++++++++++++++ tour_of_go/l4/exercise-maps.go | 26 ++++++++++++++ tour_of_go/l4/exercise-slices.go | 26 ++++++++++++++ tour_of_go/l4/function-closures.go | 20 +++++++++++ tour_of_go/l4/function-values.go | 21 +++++++++++ tour_of_go/l4/go.mod | 5 +++ tour_of_go/l4/go.sum | 2 ++ tour_of_go/l4/maps.go | 35 ++++++++++++++++++ tour_of_go/l4/mutating-maps.go | 16 +++++++++ 10 files changed, 179 insertions(+), 39 deletions(-) delete mode 100644 tour_of_go/l4/2 create mode 100644 tour_of_go/l4/exercise-fibonacci-closure.go create mode 100644 tour_of_go/l4/exercise-maps.go create mode 100644 tour_of_go/l4/exercise-slices.go create mode 100644 tour_of_go/l4/function-closures.go create mode 100644 tour_of_go/l4/function-values.go create mode 100644 tour_of_go/l4/go.mod create mode 100644 tour_of_go/l4/go.sum create mode 100644 tour_of_go/l4/maps.go create mode 100644 tour_of_go/l4/mutating-maps.go diff --git a/tour_of_go/l4/2 b/tour_of_go/l4/2 deleted file mode 100644 index fda8946..0000000 --- a/tour_of_go/l4/2 +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import "fmt" - -func main() { - var aa [2]string - - aa[0] = "Hello" - aa[1] = "World" - fmt.Println(aa[0], aa[1]) - fmt.Println(aa) - - primes := [6]int{2, 3, 5, 7, 11, 13} - fmt.Println(primes) - - var s []int = primes[1:4] - fmt.Printf("The primes are %d and the slice is %d", primes, s) - - // Slices are references to array sections - // Changing elements in a slice results in changes to the array - // So other slices referencing the same array section will also be changed - - names := [4]string{ - "John", - "Paul", - "George", - "Ringo", - } - fmt.Println(names) - - a := names[0:2] - b := names[1:3] - fmt.Println(a, b) - - // Now make some changes - b[0] = "XXX" - fmt.Println(a, b) - fmt.Println(names) -} diff --git a/tour_of_go/l4/exercise-fibonacci-closure.go b/tour_of_go/l4/exercise-fibonacci-closure.go new file mode 100644 index 0000000..ce71575 --- /dev/null +++ b/tour_of_go/l4/exercise-fibonacci-closure.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +func fibonacci() func() int { + previous := 0 + current := 0 + result := 0 + return func() int { + if current == 0 { + current = 1 + } else if current == 1 && result == 0 { + result = current + } else { + result = previous + current + previous = current + current = result + } + return result + } +} + +func main() { + f := fibonacci() + for i := 0; i < 10; i++ { + fmt.Println(f()) + } +} diff --git a/tour_of_go/l4/exercise-maps.go b/tour_of_go/l4/exercise-maps.go new file mode 100644 index 0000000..7af13ff --- /dev/null +++ b/tour_of_go/l4/exercise-maps.go @@ -0,0 +1,26 @@ +package main + +import ( + "golang.org/x/tour/wc" + "strings" +) + +func WordCount(s string) map[string]int { + result := map[string]int{} + fields := strings.Fields(s) + for i := range fields { + word := fields[i] + _, exists := result[word] + if exists == true { + result[word] += 1 + } else { + result[word] = 1 + } + } + return result +} + +func main() { + wc.Test(WordCount) +} + diff --git a/tour_of_go/l4/exercise-slices.go b/tour_of_go/l4/exercise-slices.go new file mode 100644 index 0000000..d3fc755 --- /dev/null +++ b/tour_of_go/l4/exercise-slices.go @@ -0,0 +1,26 @@ +package main + +import ( + "golang.org/x/tour/pic" + "fmt" +) + +func Pic(dx, dy int) [][]uint8 { + pic := make([][]uint8, dy) + for i := range(dy) { + inner_slice := make([]uint8, dx) + for y := range(dx) { + res := i * y + inner_slice[y] = uint8(res) + + } + pic[i] = inner_slice + } + return pic +} + +func main() { + s := Pic(8, 8) + fmt.Println(s) + pic.Show(Pic) +} diff --git a/tour_of_go/l4/function-closures.go b/tour_of_go/l4/function-closures.go new file mode 100644 index 0000000..08c20b0 --- /dev/null +++ b/tour_of_go/l4/function-closures.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func adder() func(int) int { + sum := 0 + return func(x int) int { + sum += x + return sum + } +} + +func main() { + pos, neg := adder(), adder() + for i := 0; i < 10; i++ { + fmt.Println( + pos(i), neg(-2*i), + ) + } +} diff --git a/tour_of_go/l4/function-values.go b/tour_of_go/l4/function-values.go new file mode 100644 index 0000000..73ad407 --- /dev/null +++ b/tour_of_go/l4/function-values.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "math" +) + +func compute(fn func(float64, float64) float64) float64 { + return fn(3,4) +} + +func main() { + hypot := func(x, y float64) float64 { + return math.Sqrt(x*x + y*y) + } + + fmt.Println(hypot(5, 12)) + + fmt.Println(compute(hypot)) + fmt.Println(compute(math.Pow)) +} diff --git a/tour_of_go/l4/go.mod b/tour_of_go/l4/go.mod new file mode 100644 index 0000000..2811f45 --- /dev/null +++ b/tour_of_go/l4/go.mod @@ -0,0 +1,5 @@ +module exercise-maps.go + +go 1.24.5 + +require golang.org/x/tour v0.1.0 diff --git a/tour_of_go/l4/go.sum b/tour_of_go/l4/go.sum new file mode 100644 index 0000000..cde48ea --- /dev/null +++ b/tour_of_go/l4/go.sum @@ -0,0 +1,2 @@ +golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE= +golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc= diff --git a/tour_of_go/l4/maps.go b/tour_of_go/l4/maps.go new file mode 100644 index 0000000..8192c54 --- /dev/null +++ b/tour_of_go/l4/maps.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +type Vertex struct { + Lat, Long float64 +} + +var m map[string]Vertex +var new_m = map[string]Vertex{ + "Bell Labs": Vertex{ + 40.68433, -74.39967, + }, + "Google": Vertex{ + 37.42202, -122.08408, + }, +} + +// The above can also be written +var new_new_m = map[string]Vertex{ + "Bell Labs": {40.68433, -74.39967}, + "Google": {37.42202, -122.08408}, +} + +func main() { + m = make(map[string]Vertex) + m["Bell Labs"] = Vertex{ + 40.68433, -74.39967, + } + fmt.Println(m["Bell Labs"]) + + + fmt.Println(new_m) + fmt.Println(new_new_m) +} diff --git a/tour_of_go/l4/mutating-maps.go b/tour_of_go/l4/mutating-maps.go new file mode 100644 index 0000000..ce047d9 --- /dev/null +++ b/tour_of_go/l4/mutating-maps.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +runc main() { + m := make(map[string]int) + + m["Answer"] = 42 + fmt.Println("The value:", m["Answer"]) + + delete(m, "Answer") + fmt.Println("The value:", m["Answer"]) + + v, ok := m["Answer"] + fmt.Println("The value:", v, "Present?", ok) +}