Sync
This commit is contained in:
		
							parent
							
								
									cd080ddda2
								
							
						
					
					
						commit
						ba7812bfdd
					
				@ -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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								tour_of_go/l5/exercise-images/exercise-images.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								tour_of_go/l5/exercise-images/exercise-images.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								tour_of_go/l5/exercise-images/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tour_of_go/l5/exercise-images/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
module exercise-images.go
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
 | 
			
		||||
require golang.org/x/tour v0.1.0
 | 
			
		||||
							
								
								
									
										2
									
								
								tour_of_go/l5/exercise-images/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								tour_of_go/l5/exercise-images/go.sum
									
									
									
									
									
										Normal file
									
								
							@ -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=
 | 
			
		||||
							
								
								
									
										12
									
								
								tour_of_go/l5/images.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tour_of_go/l5/images.go
									
									
									
									
									
										Normal file
									
								
							@ -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())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								tour_of_go/l6/generics.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								tour_of_go/l6/generics.go
									
									
									
									
									
										Normal file
									
								
							@ -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())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								tour_of_go/l6/index.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								tour_of_go/l6/index.go
									
									
									
									
									
										Normal file
									
								
							@ -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"))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								tour_of_go/l7/buffered-channels.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tour_of_go/l7/buffered-channels.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								tour_of_go/l7/channels.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								tour_of_go/l7/channels.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								tour_of_go/l7/default-selection.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								tour_of_go/l7/default-selection.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								tour_of_go/l7/exercise-binary-tree.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								tour_of_go/l7/exercise-binary-tree.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								tour_of_go/l7/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tour_of_go/l7/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
module exercise-binary-tree.go
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
 | 
			
		||||
require golang.org/x/tour v0.1.0
 | 
			
		||||
							
								
								
									
										2
									
								
								tour_of_go/l7/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								tour_of_go/l7/go.sum
									
									
									
									
									
										Normal file
									
								
							@ -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=
 | 
			
		||||
							
								
								
									
										18
									
								
								tour_of_go/l7/goroutines.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								tour_of_go/l7/goroutines.go
									
									
									
									
									
										Normal file
									
								
							@ -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")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								tour_of_go/l7/mutex-couter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								tour_of_go/l7/mutex-couter.go
									
									
									
									
									
										Normal file
									
								
							@ -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"))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								tour_of_go/l7/range-and-close.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tour_of_go/l7/range-and-close.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								tour_of_go/l7/select.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								tour_of_go/l7/select.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user