開発環境
- 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)の解答を求めてみる。
コード
landmark.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
}
sample3.go
package main
import (
"fmt"
"heafirstgo/geo"
"log"
)
func main() {
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)
}
fmt.Println(location.Name())
fmt.Println(location.Latitude())
fmt.Println(location.Longitude())
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ go run sample3.go The Googleplex 37.42 -122.08 $
0 コメント:
コメントを投稿