diff --git a/tour_of_go/l5/3 b/tour_of_go/l5/3 deleted file mode 100644 index 3e392fb..0000000 --- a/tour_of_go/l5/3 +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import "fmt" - -func main() { - var i interface{} = "hello" - - s := i.(string) - fmt.Println(s) - - fmt.Printf("%v- %T\n", s, s) -} diff --git a/tour_of_go/l5/exercise-images/exercise-images.go b/tour_of_go/l5/exercise-images/exercise-images.go new file mode 100644 index 0000000..e0727a4 --- /dev/null +++ b/tour_of_go/l5/exercise-images/exercise-images.go @@ -0,0 +1,29 @@ +package main + +import ( + "golang.org/x/tour/pic" + "image" + "image/color" +) + +type Image struct{ + w, h int +} + +func (i Image) Bounds() image.Rectangle { + return image.Rect(0, 0, i.w, i.h) +} + +func (i Image) ColorModel() color.Model { + return color.RGBAModel +} + +func (i Image) At(x, y int) color.Color { + v := uint8(x^y) + return color.RGBA{v, v, 255, 255} +} + +func main() { + m := Image{320, 240} + pic.ShowImage(m) +} diff --git a/tour_of_go/l5/exercise-images/go.mod b/tour_of_go/l5/exercise-images/go.mod new file mode 100644 index 0000000..f157c6c --- /dev/null +++ b/tour_of_go/l5/exercise-images/go.mod @@ -0,0 +1,5 @@ +module exercise-images.go + +go 1.24.5 + +require golang.org/x/tour v0.1.0 diff --git a/tour_of_go/l5/exercise-images/go.sum b/tour_of_go/l5/exercise-images/go.sum new file mode 100644 index 0000000..cde48ea --- /dev/null +++ b/tour_of_go/l5/exercise-images/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/l5/images.go b/tour_of_go/l5/images.go new file mode 100644 index 0000000..42de838 --- /dev/null +++ b/tour_of_go/l5/images.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + "image" +) + +func main() { + m := image.NewRGBA(image.Rect(0, 0, 100, 100)) + fmt.Println(m.Bounds()) + fmt.Println(m.At(0, 0).RGBA()) +} diff --git a/tour_of_go/l6/generics.go b/tour_of_go/l6/generics.go new file mode 100644 index 0000000..600f4bf --- /dev/null +++ b/tour_of_go/l6/generics.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" +) + +type List[T any] struct { + next *List[T] + val T +} + +func (l *List[T]) Prepend(v T) *List[T] { + return &List[T]{next: l, val: v} +} + +func (l *List[t]) Len() int { + count := 0 + for cur := l; cur != nil; cur = cur.next { + count++ + } + return count +} + +func main() { + var test_list *List[int] + fmt.Println(test_list) + test_list = test_list.Prepend(10) + fmt.Println(test_list) + test_list = test_list.Prepend(20) + fmt.Println(test_list) + fmt.Printf("The list has %d items\n", test_list.Len()) + test_list = test_list.Prepend(30) + fmt.Println(test_list) + + fmt.Printf("The list has %d items", test_list.Len()) +} diff --git a/tour_of_go/l6/index.go b/tour_of_go/l6/index.go new file mode 100644 index 0000000..d8e9f38 --- /dev/null +++ b/tour_of_go/l6/index.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func Index[T comparable](s []T, x T) int { + for i, v := range s { + if v == x { + return i + } + } + return -1 +} + +func main() { + si := []int{10, 20, 15, -10} + fmt.Println(Index(si, 15)) + + ss := []string{"foo", "bar", "bax"} + fmt.Println(Index(ss, "hello")) +} diff --git a/tour_of_go/l7/buffered-channels.go b/tour_of_go/l7/buffered-channels.go new file mode 100644 index 0000000..8b1e42b --- /dev/null +++ b/tour_of_go/l7/buffered-channels.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + ch := make(chan int, 2) + ch <- 1 + ch <- 2 + // ch <- 3 + fmt.Println(<-ch) + fmt.Println(<-ch) +} diff --git a/tour_of_go/l7/channels.go b/tour_of_go/l7/channels.go new file mode 100644 index 0000000..3b02a3f --- /dev/null +++ b/tour_of_go/l7/channels.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func sum(s []int, c chan int) { + sum := 0 + for _, v := range s { + sum += v + } + c <- sum // Send the sum into the channel +} + +func main() { + s := []int{7, 2, 8, -8, 4, 0} + + c := make(chan int) + go sum(s[:len(s)/2], c) + go sum(s[len(s)/2:], c) + x, y := <-c, <-c // Receive from the channel + + fmt.Println(x, y, x+y) +} diff --git a/tour_of_go/l7/default-selection.go b/tour_of_go/l7/default-selection.go new file mode 100644 index 0000000..ac8afe8 --- /dev/null +++ b/tour_of_go/l7/default-selection.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + start := time.Now() + tick := time.Tick(100 * time.Millisecond) + boom := time.After(500 * time.Millisecond) + elapsed := func() time.Duration { + return time.Since(start).Round(time.Millisecond) + } + for { + select { + case <- tick: + fmt.Printf("[%6s] tick.\n", elapsed()) + case <- boom: + fmt.Printf("[%6s] BOOM!\n", elapsed()) + return + default: + fmt.Printf("[%6s] .\n", elapsed()) + time.Sleep(50 * time.Millisecond) + } + } +} diff --git a/tour_of_go/l7/exercise-binary-tree.go b/tour_of_go/l7/exercise-binary-tree.go new file mode 100644 index 0000000..b572359 --- /dev/null +++ b/tour_of_go/l7/exercise-binary-tree.go @@ -0,0 +1,58 @@ +package main + +import ( + "golang.org/x/tour/tree" + "fmt" +) + +// type Tree struct { +// Left *Tree +// Value int +// Right *Tree +// } + +func Walk(t *tree.Tree, ch chan int) { + defer close(ch) + var walker func(t *tree.Tree) + walker = func (t *tree.Tree) { + if t == nil { + return + } + walker(t.Left) + ch <- t.Value + walker(t.Right) + } + walker(t) +} + +func Same(t1, t2 *tree.Tree) bool { + ch1 := make(chan int) + ch2 := make(chan int) + + go Walk(t1, ch1) + go Walk(t2, ch2) + for i := range ch1 { + v := <- ch2 + if i != v { + return false + } + } + return true +} + +func main() { + ch := make(chan int) + fmt.Println("START") + // Create two trees + t1 := tree.New(2) + t2 := tree.New(2) + t3 := tree.New(3) + go Walk(t1, ch) + for i := range ch { + fmt.Println(i) + } + + same_1 := Same(t1, t2) + same_2 := Same(t2, t3) + fmt.Println(same_1, same_2) +} diff --git a/tour_of_go/l7/go.mod b/tour_of_go/l7/go.mod new file mode 100644 index 0000000..637c8f1 --- /dev/null +++ b/tour_of_go/l7/go.mod @@ -0,0 +1,5 @@ +module exercise-binary-tree.go + +go 1.24.5 + +require golang.org/x/tour v0.1.0 diff --git a/tour_of_go/l7/go.sum b/tour_of_go/l7/go.sum new file mode 100644 index 0000000..cde48ea --- /dev/null +++ b/tour_of_go/l7/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/l7/goroutines.go b/tour_of_go/l7/goroutines.go new file mode 100644 index 0000000..d211a53 --- /dev/null +++ b/tour_of_go/l7/goroutines.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "time" +) + +func say(s string) { + for i := 0; i < 5; i++ { + time.Sleep(100 * time.Millisecond) + fmt.Println(s) + } +} + +func main() { + go say("hello") + say("world") +} diff --git a/tour_of_go/l7/mutex-couter.go b/tour_of_go/l7/mutex-couter.go new file mode 100644 index 0000000..e778725 --- /dev/null +++ b/tour_of_go/l7/mutex-couter.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "sync" + "time" +) + +type SafeCounter struct { + mu sync.Mutex + v map[string]int +} + +func (c *SafeCounter) Inc(key string) { + c.mu.Lock() + c.v[key]++ + c.mu.Unlock() +} + +func (c *SafeCounter) Value(key string) int { + c.mu.Lock() + defer c.mu.Unlock() + return c.v[key] +} + +func main() { + c := SafeCounter{v: make(map[string]int)} + for i := 0; i < 1000; i++ { + go c.Inc("somekey") + } + time.Sleep(time.Second) + fmt.Println(c.Value("somekey")) +} diff --git a/tour_of_go/l7/range-and-close.go b/tour_of_go/l7/range-and-close.go new file mode 100644 index 0000000..9a75d62 --- /dev/null +++ b/tour_of_go/l7/range-and-close.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" +) + +func fibonacci(n int, c chan int) { + x, y := 0, 1 + for i := 0; i < n; i++ { + c <- x + x, y = y, x+y + } + close(c) +} + +func main() { + c := make(chan int, 10) + fmt.Printf("The channel's capacity is %d", cap(c)) + go fibonacci(cap(c), c) + for i := range c { + fmt.Println(i) + } +} diff --git a/tour_of_go/l7/select.go b/tour_of_go/l7/select.go new file mode 100644 index 0000000..21a55a4 --- /dev/null +++ b/tour_of_go/l7/select.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +func fibonacci(c, quit chan int) { + x, y := 0, 1 + for { + select { + case c <- x: + x,y = y, x+y + case <- quit: + fmt.Println("quit") + return + } + } +} + +func main() { + c := make(chan int) + quit := make(chan int) + go func() { + for i := 0; i < 10; i++ { + fmt.Println(<-c) + } + quit <- 0 + }() + fibonacci(c, quit) +}