2019年7月19日金曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 5(on the list - Arrays)、Exercise(154)の解答を求めてみる。

コード

sample1_test.go

package main

import "testing"

func TestArrayInts(t *testing.T) {
 var numbers [3]int
 numbers[0] = 42
 numbers[2] = 108

 wants := []int{42, 0, 108}
 for i, want := range wants {
  got := numbers[i]
  if got != want {
   t.Errorf("number[%d] = %d, want %d", i, got, want)
  }
 }
}

func TestArrayStrings(t *testing.T) {
 var letters = []string{"a", "b", "c"}
 indices := []int{2, 0, 1}
 wants := []string{"c", "a", "b"}
 for i, want := range wants {
  index := indices[i]
  got := letters[index]
  if got != want {
   t.Errorf("letters[%d] = %s, want %s", index, got, want)
  }
 }
}

sample1.go

package main

func main() {}

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

$ go test
PASS
ok   _/.../go/Head_First_Go/ch5/sample1 0.005s
$ go test -v
=== RUN   TestArrayInts
--- PASS: TestArrayInts (0.00s)
=== RUN   TestArrayStrings
--- PASS: TestArrayStrings (0.00s)
PASS
ok   _/.../go/Head_First_Go/ch5/sample1 0.005s
$ 

0 コメント:

コメントを投稿