2019年7月26日金曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 7(labeling data - Maps)、Exercise(217)の解答を求めてみる。

コード

sample2_test.go

package main

import "testing"

func Test(t *testing.T) {
 data := []string{"a", "c", "e", "a", "e"}
 counts := make(map[string]int)
 for _, item := range data {
  counts[item]++
 }
 letters := []string{"a", "b", "c", "d", "e"}
 want_counts := []int{2, 0, 1, 0, 2}
 want_oks := []bool{true, false, true, false, true}
 for i, letter := range letters {
  count, ok := counts[letter]
  want_count := want_counts[i]
  if count != want_count {
   t.Errorf("count: %d, want %d", count, want_count)
  }
  want_ok := want_oks[i]
  if ok != want_ok {
   t.Errorf("ok: %v, want %v", ok, want_ok)
  }
 }
}

sample2.go

package main

func main() {}

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

$ go test
PASS
ok   _/.../go/Head_First_Go/ch7/sample2 0.005s
$ 

0 コメント:

コメントを投稿