開発環境
- macOS High Sierra - Apple (OS)
- Emacs (Text Editor)
- Go (プログラミング言語)
- Introducing Go: Build Reliable, Scalable Programs Caleb Doxsey (著) (参考書籍)
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド - 独自の構造を使う)、長いエクササイズ(p. 228)をGoで取り組んでみる。
長いエクササイズ(p. 228)
コード(Emacs)
package main
import "fmt"
type Exercise struct {
description string
duration float64
}
type Meal struct {
ingredients string
weight float64
}
type Preferences struct {
Meal
Exercise
}
type Fish struct {
name string
species string
teeth int
age int
Preferences
}
func label(f Fish) {
fmt.Printf("名前: %s\n種類: %s\n%d本の歯、%d才\n"+
"餌は%2.2fキロの%sを与え、%sを%2.2f時間行わせます。\n",
f.name, f.species, f.teeth, f.age,
f.weight, f.ingredients, f.description, f.duration)
}
func main() {
snappy := Fish{
"スナッピー", "ピラニア", 69, 4,
Preferences{
Meal{"肉", 0.1},
Exercise{"ジャグジーでの泳ぎ", 7.5},
},
}
label(snappy)
}
入出力結果(Terminal)
$ go run sample2.go 名前: スナッピー 種類: ピラニア 69本の歯、4才 餌は0.10キロの肉を与え、ジャグジーでの泳ぎを7.50時間行わせます。 $
0 コメント:
コメントを投稿