0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

25行で分かれgo言語における変数のスコープとシャドウイング

Posted at
package main

import (
	"fmt"
)

var i = 0

func main() {
	fmt.Println(i) // 0
	i := 1
	fmt.Println(i) // 1
	{
		fmt.Println(i) // 1
		i := 2
		fmt.Println(i) // 2
		for i := 3; i < 4; i++ {
			fmt.Println(i) // 3
			i := 4
			fmt.Println(i) // 4
			if i := i + 1; i == 5 {
				fmt.Println(i) // 5
				i := 6
				fmt.Println(i) // 6
			}
			fmt.Println(i) // 4
		}
		fmt.Println(i) // 2
	}
	fmt.Println(i) // 1
}

のっけからコードをベタ張りするスタイル。グローバル変数の定義からメイン関数の終わりまででぴったり25行です。

  1. グローバル
  2. メイン関数
  3. ブロック
  4. for文
  5. for文の1ループ
  6. if文
  7. if文の一つの場合

これでわかるつもりで書いていますがわからない場合はコメントください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?