2019年8月4日日曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 9(you're my type - Defined Types)、Exercise(278)の解答を求めてみる。

コード

sample2_test.go

package main

import "testing"

func TestAdd(t *testing.T) {
 wants := []int{14, 7}
 ten := Number(10)
 four := Number(4)
 gots := []int{ten.Add(4), four.Add(3)}
 for i, want := range wants {
  got := gots[i]
  if got != want {
   t.Errorf("got %d, want %d", got, want)
  }
 }
}
func TestSubtract(t *testing.T) {
 wants := []int{5, 2}
 ten := Number(10)
 four := Number(4)
 gots := []int{ten.Subtract(5), four.Subtract(2)}
 for i, want := range wants {
  got := gots[i]
  if got != want {
   t.Errorf("got %d, want %d", got, want)
  }
 }
}

sample2.go

package main

type Number int

func (n *Number) Add(otherNumber int) int {
 return int(*n) + otherNumber
}
func (n *Number) Subtract(otherNumber int) int {
 return int(*n) - otherNumber
}
func main() {}

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

$ go test
# _/.../go/Head_First_Go/ch9/sample2 [_/.../go/Head_First_Go/ch9/sample2.test]
./sample2_test.go:7:9: undefined: Number
./sample2_test.go:8:10: undefined: Number
./sample2_test.go:19:9: undefined: Number
./sample2_test.go:20:10: undefined: Number
FAIL _/.../go/Head_First_Go/ch9/sample2 [build failed]
$ go test
PASS
ok   _/.../go/Head_First_Go/ch9/sample2 0.004s
$ 

0 コメント:

コメントを投稿