"fmt" func main() { var n int var s string n = 10 s = "foo" var m = n + 1 fmt.Printf("%d %d %s\n", n, m, s) // Output: 10 11 foo } package main import "fmt" func main() { n := 10 // লུมએݴ s := "foo" // ʏ m := n + 1 // ʏ fmt.Printf("%d %d %s\n", n, m, s) // Output: 10 11 foo }
_, i := range list { sum += i } return sum } type sumTest struct { in []int out int } var sumTests = []sumTest{ {[]int{1, 2, 3}, 6}, {[]int{}, 0}, {[]int{-1, 1}, 1}, // Θ͟ͱ FAIL } func TestSum(t *testing.T) { for i, test := range sumTests { out := Sum(test.in) if out != test.out { t.Errorf("#%d Sum(%v) = %d, want %d", i, test.in, out, test.out) } } } w ςετίʔυͷϑΝΠϧ໊ xxx_test.go YYYҙ w ςετ໊ؔTestͰ࢝·Γ Ҿʹ*testing.TΛड͚ Δ w ཧpackageݩίʔυͱ ͢Δ _testΛ͚ͭΔ
chan bool) { fmt.Println("f1") done <- true } func f2(done chan bool) { fmt.Println("f2") done <- true } func main() { done1 := make(chan bool) done2 := make(chan bool) go f1(done1) go f2(done2) select { case <-done1: fmt.Println("f1 is first!") case <-done2: fmt.Println(“f2 is first!") } } $ go run main.go f1 f1 is first! f2 $ go run main.go f2 f2 is first!
for i := 1; i <= 3; i++ { counter <- i time.Sleep(100 * time.Microsecond) } close(counter) } func main() { counter := make(chan int) go async(counter) for count := range counter { fmt.Println(count) } } $ go run main.go 1 2 3 $