which code runs next - Conditionals and Loops - Blocks and variable scope
Head First Go、 Jay McGavren(著)、O’Reilly Media)の Chapter 2(which code runs next? Conditionals and Loops)、p.53(Exercise)の解答を求めてみる。
コード
sample.go
package main
import "fmt"
var a = "a"
func main() {
	a = "a"
	b := "b"
	if true {
		c := "c"
		if true {
			d := "d"
			fmt.Println(a)
			fmt.Println(b)
			fmt.Println(c)
			fmt.Println(d)
		}
		fmt.Println(a)
		fmt.Println(b)
		fmt.Println(c)
		// fmt.Println(d)
	}
	fmt.Println(a)
	fmt.Println(b)
	// fmt.Println(c)
	// fmt.Println(d)
}
入出力結果(Terminal, Zsh)
% go run ./sample.go 
a
b
c
d
a
b
c
a
b
%
