2019年7月24日水曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 6(appending issue - Slice)、Code Magnets(198)の解答を求めてみる。

コード

sample3_test.go

package main

import "testing"

func TestSum(t *testing.T) {
 wants := []int{16, 7}
 gots := []int{sum(7, 9), sum(1, 2, 4)}
 for i, want := range wants {
  got := gots[i]
  if got != want {
   t.Errorf("sum: %d, want %d", got, want)
  }
 }
 gots = []int{sum(1, 2, 4, 9), sum(7)}
 for i, want := range wants {
  got := gots[i]
  if got != want {
   t.Errorf("sum: %d, want %d", got, want)
  }
 }
}

sample3.go

package main

func sum(numbers ...int) int {
 sum := 0
 for _, number := range numbers {
  sum += number
 }
 return sum
}
func main() {}

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

$ go test
PASS
ok   _/.../go/Head_First_Go/ch6/sample3 0.005s
$ 

0 コメント:

コメントを投稿