building storage - Structs - pointers
Head First Go、 Jay McGavren(著)、O’Reilly Media)の Chapter 8(building storage - Structs)、p.247(Exercise)の解答を求めてみる。
コード
main.go
package main
import "fmt"
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() {
var mustang car
mustang.name = "Mustang Cobra"
mustang.topSpeed = 225
nitroBoost(&mustang)
fmt.Println(mustang.name)
fmt.Println(mustang.topSpeed)
var fuses part
fuses.description = "Fuses"
fuses.count = 5
doublePack(&fuses)
fmt.Println(fuses.description)
fmt.Println(fuses.count)
}
入出力結果(Terminal, Zsh)
% go run ./main.go
Mustang Cobra
275
Fuses
10
%