開発環境
- 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 10(keep it to yourself - Encapsulation and Embedding)、Exercise(314)の解答を求めてみる。
コード
~/go/src/headfirstgo/geo/lanrdmark.go
package geo
import "errors"
type Landmark struct {
name string
Coordinates
}
func (l *Landmark) Name() string {
return l.name
}
func (l *Landmark) SetName(name string) error {
if name == "" {
return errors.New("invalid name")
}
l.name = name
return nil
}
コード
sample2_test.go
package main
import (
"headfirstgo/geo"
"log"
"testing"
)
func TestLandmark(t *testing.T) {
location := geo.Landmark{}
err := location.SetName("The Googleplex")
if err != nil {
log.Fatal(err)
}
err = location.SetLatitude(37.42)
if err != nil {
log.Fatal(err)
}
err = location.SetLongitude(-122.08)
if err != nil {
log.Fatal(err)
}
want1 := "The Googleplex"
got1 := location.Name()
if got1 != want1 {
t.Errorf("got %v, want %v", got1, want1)
}
want2 := 37.42
got2 := location.Latitude()
if got2 != want2 {
t.Errorf("got %v, want %v", got2, want2)
}
want3 := -122.08
got3 := location.Longitude()
if got3 != want3 {
t.Errorf("got %v, want %v", got3, want3)
}
}
sample2.go
package main
func main() {}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ go test PASS ok _/.../go/Head_First_Go/ch10/sample2 0.006s $
0 コメント:
コメントを投稿