開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 4(bundles of code - Packages)、Pool Puzzle(122)の解答を求めてみる。
コード
~/go/src/calc/calc.go
package calc func Add(first float64, second float64) float64 { return first + second } func Subtract(first float64, second float64) float64 { return first - second }
sample1_test.go
package main import ( "calc" "testing" ) func TestAdd(t *testing.T) { want := 3.0 got := calc.Add(1, 2) if got != want { t.Errorf("Add(1, 2) = %v, want %v", got, want) } } func TestSub(t *testing.T) { want := 4.0 got := calc.Subtract(7, 3) if got != want { t.Errorf("Subtract(7, 3) = %v, want %v", got, want) } }
sample1.go
package main func main() {}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ go test # _/.../go/Head_First_Go/ch4/sample1 sample1_test.go:4:2: cannot find package "calc" in any of: /.../go/src/calc (from $GOROOT) /.../go/src/calc (from $GOPATH) FAIL _/.../go/Head_First_Go/ch4/sample1 [setup failed] $ go test # _/.../go/Head_First_Go/ch4/sample1 [_/.../go/Head_First_Go/ch4/sample1.test] ./sample1_test.go:11:9: invalid operation: got != want (mismatched types float64 and int) ./sample1_test.go:18:9: invalid operation: got != want (mismatched types float64 and int) FAIL _/.../go/Head_First_Go/ch4/sample1 [build failed] $ go test PASS ok _/.../go/Head_First_Go/ch4/sample1 0.004s $
0 コメント:
コメントを投稿