すぐ見れるようにメモしておく
for
for i := 0; i < 10; i++ {
sum += i
}
for (range)
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
for i, v := range pow {
fmt.Printf("2**%d = %d\n", i, v)
}
if
if x < 0 {
return sqrt(-x) + "i"
}
if short style
if v := math.Pow(x, n); v < lim {
return v
}
if and else
if v := math.Pow(x, n); v < lim {
return v
} else {
fmt.Printf("%g >= %g\n", v, lim)
}
Switch
switch
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.\n", os)
}
switch with no condition
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
Defer
defer
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
文字列のフォーマット
text := fmt.Sprintf("id: %d, value: %s", index, value)