開発環境
- 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 9(you're my type - Defined Types)、Exercise(278)の解答を求めてみる。
コード
sample3_test.go
package main
import "testing"
func TestToMillilitrs(t *testing.T) {
l := Liters(3)
want := MilliLiters(3000)
got := l.ToMilliLiters()
if got != want {
t.Errorf("got %f, want %f", got, want)
}
}
func TestToLiters(t *testing.T) {
ml := MilliLiters(500)
want := Liters(0.5)
got := ml.ToLiters()
if got != want {
t.Errorf("got %f, want %f", got, want)
}
}
sample3.go
package main
type Liters float64
type MilliLiters float64
func (l Liters) ToMilliLiters() MilliLiters {
return MilliLiters(l * 1000)
}
func (m MilliLiters) ToLiters() Liters {
return Liters(m / 1000)
}
func main() {}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ go test # _/.../go/Head_First_Go/ch9/sample3 [_/.../go/Head_First_Go/ch9/sample3.test] ./sample3_test.go:6:7: undefined: Liters ./sample3_test.go:15:8: undefined: MilliLiters FAIL _/.../go/Head_First_Go/ch9/sample3 [build failed] $ go test # _/.../go/Head_First_Go/ch9/sample3 [_/.../go/Head_First_Go/ch9/sample3.test] ./sample3_test.go:8:10: l.ToMilliLiters undefined (type Liters has no field or method ToMilliLiters) ./sample3_test.go:17:11: ml.ToLiters undefined (type MilliLiters has no field or method ToLiters) FAIL _/.../go/Head_First_Go/ch9/sample3 [build failed] $ go test # _/.../go/Head_First_Go/ch9/sample3 [_/.../go/Head_First_Go/ch9/sample3.test] ./sample3_test.go:9:9: invalid operation: got != want (mismatched types MilliLiters and float64) ./sample3_test.go:18:9: invalid operation: got != want (mismatched types Liters and float64) FAIL _/.../go/Head_First_Go/ch9/sample3 [build failed] $ go test PASS ok _/.../go/Head_First_Go/ch9/sample3 0.005s $
0 コメント:
コメントを投稿