2019年8月16日金曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 13(sharing work - Goroutines and Channels)、Exercise(395)の解答を求めてみる。

コード

sample2_test.go

package main

import "testing"

func TestChennel(t *testing.T) {
 channelA := make(chan int)
 channelB := make(chan int)
 go odd(channelA)
 go even(channelB)
 wants := []int{1, 3, 2, 4}
 gots := []int{<-channelA, <-channelA, <-channelB, <-channelB}
 for i, want := range wants {
  got := gots[i]
  if got != want {
   t.Errorf("got %d, want %d", got, want)
  }
 }
}

sample2.go

package main

func odd(channel chan int) {
 channel <- 1
 channel <- 3
}
func even(channel chan int) {
 channel <- 2
 channel <- 4
}
func main() {}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ go test
# _/.../go/Head_First_Go/ch13/sample2 [_/.../go/Head_First_Go/ch13/sample2.test]
./sample2_test.go:8:5: undefined: odd
./sample2_test.go:9:5: undefined: even
FAIL _/.../go/Head_First_Go/ch13/sample2 [build failed]
$ go test
PASS
ok   _/.../go/Head_First_Go/ch13/sample2 0.005s
$ 

0 コメント:

コメントを投稿