開発環境
- 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 8(building storage - Structs)、Exercise(247)の解答を求めてみる。
コード
sample3_test.go
package main
import "testing"
func TestCar(t *testing.T) {
var mustang car
mustang.name = "Mustang Cobra"
mustang.topSpeed = 225
nitroBoost(&mustang)
want1 := "Mustang Cobra"
got1 := mustang.name
if got1 != want1 {
t.Errorf("name = %s, want %s", got1, want1)
}
want2 := 275.0
got2 := mustang.topSpeed
if got2 != want2 {
t.Errorf("topSpeed = %v, want %v", got2, want2)
}
}
func TestPart(t *testing.T) {
var fuses part
fuses.description = "Fuses"
fuses.count = 5
doublePack(&fuses)
want1 := "Fuses"
got1 := fuses.description
if got1 != want1 {
t.Errorf("description = %v, want %v", got1, want1)
}
want2 := 10
got2 := fuses.count
if got2 != want2 {
t.Errorf("count = %v, want %v", got2, want2)
}
}
sample3.go
package main
type car struct {
name string
topSpeed float64
}
func nitroBoost(c *car) {
c.topSpeed += 50
}
type part struct {
description string
count int
}
func doublePack(p *part) {
p.count *= 2
}
func main() {}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ go test
# _/.../go/Head_First_Go/ch8/sample3 [_/.../go/Head_First_Go/ch8/sample3.test]
./sample3_test.go:6:14: undefined: car
./sample3_test.go:9:2: undefined: nitroBoost
./sample3_test.go:24:12: undefined: part
./sample3_test.go:27:2: undefined: doublePack
FAIL _/.../go/Head_First_Go/ch8/sample3 [build failed]
$ go test
--- FAIL: TestCar (0.00s)
sample3_test.go:19: topSpeed = 225, want 275
--- FAIL: TestPart (0.00s)
sample3_test.go:36: count = 5, want 10
FAIL
exit status 1
FAIL _/.../go/Head_First_Go/ch8/sample3 0.005s
$ go test
# _/.../go/Head_First_Go/ch8/sample3 [_/.../go/Head_First_Go/ch8/sample3.test]
./sample3_test.go:9:12: cannot use mustang (type car) as type *car in argument to nitroBoost
./sample3_test.go:27:12: cannot use fuses (type part) as type *part in argument to doublePack
FAIL _/.../go/Head_First_Go/ch8/sample3 [build failed]
$ go test
PASS
ok _/.../go/Head_First_Go/ch8/sample3 0.005s
$
0 コメント:
コメントを投稿