현재 3권의 신간들인 Go Optimizations 101, Go Details & Tips 101Go Generics 101이 출간되어 있습니다. Leanpub 서점에서 번들을 모두 구입하시는 방법이 비용 대비 효율이 가장 좋습니다.

Go에 대한 많은 정보들과 Go 101 책들의 최신 소식을 얻으시려면 Go 101 트위터 계정인 @Go100and1을 팔로잉 해주세요.

Code Blocks and Identifier Scopes

This article will explain the code blocks and identifier scopes in Go.

(Please note, the definitions of code block hierarchies in this article are a little different from the viewpoint of go/* standard packages.)

Code Blocks

In a Go project, there are four kinds of code blocks (also called blocks later): Some keywords in all kinds of control flows are followed by some implicit code blocks.

The local blocks which aren't nested in any other local blocks are called top-level (or package-level) local blocks. Top-level local blocks are all function bodies.

Note, the input parameters and output results of a function are viewed as being declared in explicit body code block of the function, even if their declarations stay out of the pair of braces enclosing the function body block.

Code block hierarchies:

(The differences to Go specification are to make the below explanations for identifier shadowing simpler.)

Here is a picture shows the block hierarchies in a program:
code block hierarchies

Code blocks are mainly used to explain allowed declaration positions and scopes of source code element identifiers.

Source Code Element Declaration Places

There are six kinds of source code elements which can be declared:

Labels are used in the break, continue, and goto statements.

A declaration binds a non-blank identifier to a source code element (constant, type, variable, function, label, or package). In other words, in the declaration, the declared source code element is named as the non-blank identifier. After the declaration, we can use the non-blank identifier to represent the declared source code element.

The following table shows which code blocks all kinds of source code elements can be directly declared in:
the universe block package blocks file blocks local blocks
predeclared (built-in elements) (1) Yes
package imports Yes
defined types and type alias (non-builtin) Yes Yes Yes
named constants (non-builtin) Yes Yes Yes
variables (non-builtin) (2) Yes Yes Yes
functions (non-builtin) Yes Yes
labels Yes

(1) predeclared elements are documented in builtin standard package.
(2) excluding struct field variables.

So, Please note,

(BTW, the go/* standard packages think file code blocks can only contain package import declarations.)

The source code elements declared in package blocks but outside of any local blocks are called package-level source code elements. Package-level source code elements can be named constants, variables, functions, defined types, or type aliases.

Scopes of Declared Source Code Elements

The scope of a declared identifier means the identifiable range of the identifier (or visible range).

Without considering identifier shadowing which will be explained in the last section of the current article, the scope definitions for the identifiers of all kinds of source code elements are listed below.

Blank identifiers have no scopes.

(Note, the predeclared iota is only visible in constant declarations.)

You may have noticed the minor difference of identifier scope definitions between local type definitions and local variables, local constants and local type aliases. The difference means a defined type may be able to reference itself in its declaration. Here is an example to show the difference.
package main

func main() {
	// var v int = v   // error: v is undefined
	// const C int = C // error: C is undefined
	/*
	type T = struct {
		*T    // error: T uses 
		x []T // error: T uses 
	}
	*/

	// Following type definitions are all valid.
	type T struct {
		*T
		x []T
	}
	type A [5]*A
	type S []S
	type M map[int]M
	type F func(F) F
	type Ch chan Ch
	type P *P

	// ...
	var p P
	p = &p
	p = ***********************p
	***********************p = p

	var s = make(S, 3)
	s[0] = s
	s = s[0][0][0][0][0][0][0][0]

	var m = M{}
	m[1] = m
	m = m[1][1][1][1][1][1][1][1]
}

Note, call fmt.Print(s) and call fmt.Print(m) both panic (for stack overflow).

And the scope difference between package-level and local declarations:
package main

// Here the two identifiers at each line are the
// same one. The right ones are both not the
// predeclared identifiers. Instead, they are
// same as respective left one. So the two
// lines both fail to compile.
/*
const iota = iota // error: constant definition loop
var true = true   // error: typechecking loop
*/

var a = b // can reference variables declared later
var b = 123

func main() {
	// The identifiers at the right side in the
	// next two lines are the predeclared ones.
	const iota = iota // ok
	var true = true   // ok
	_ = true

	// The following lines fail to compile, for
	// c references a later declared variable d.
	/*
	var c = d
	var d = 123
	_ = c
	*/
}

Identifier Shadowing

Ignoring labels, an identifier declared in an outer code block can be shadowed by the same identifier declared in code blocks nested (directly or indirectly) in the outer code block.

Labels can’t be shadowed.

If an identifier is shadowed, its scope will exclude the scopes of its shadowing identifiers.

Below is an interesting example. The code contains 6 declared variables named x. A x declared in a deeper block shadows the xs declared in shallower blocks.
package main

import "fmt"

var p0, p1, p2, p3, p4, p5 *int
var x = 9999 // x#0

func main() {
	p0 = &x
	var x = 888  // x#1
	p1 = &x
	for x := 70; x < 77; x++ {  // x#2
		p2 = &x
		x := x - 70 //  // x#3
		p3 = &x
		if x := x - 3; x > 0 { // x#4
			p4 = &x
			x := -x // x#5
			p5 = &x
		}
	}

	// 9999 888 77 6 3 -3
	fmt.Println(*p0, *p1, *p2, *p3, *p4, *p5)
}

Another example: the following program prints Sheep Goat instead of Sheep Sheep. Please read the comments for explanations.
package main

import "fmt"

var f = func(b bool) {
	fmt.Print("Goat")
}

func main() {
	var f = func(b bool) {
		fmt.Print("Sheep")
		if b {
			fmt.Print(" ")
			f(!b) // The f is the package-level f.
		}
	}
	f(true) // The f is the local f.
}

If we modify the above program as the following shown, then it will print Sheep Sheep.
func main() {
	var f func(b bool)
	f = func(b bool) {
		fmt.Print("Sheep")
		if b {
			fmt.Print(" ")
			f(!b) // The f is also the local f now.
		}
	}
	f(true)
}

For some circumstances, when identifiers are shadowed by variables declared with short variable declarations, some new gophers may get confused about whether a variable in a short variable declaration is redeclared or newly declared. The following example (which has bugs) shows the famous trap in Go. Almost every gopher has ever fallen into the trap in the early days of using Go.
package main

import "fmt"
import "strconv"

func parseInt(s string) (int, error) {
	n, err := strconv.Atoi(s)
	if err != nil {
		// Some new gophers may think err is an
		// already declared variable in the following
		// short variable declaration. However, both
		// b and err are new declared here actually.
		// The new declared err variable shadows the
		// err variable declared above.
		b, err := strconv.ParseBool(s)
		if err != nil {
			return 0, err
		}

		// If execution goes here, some new gophers
		// might expect a nil error will be returned.
		// But in fact, the outer non-nil error will
		// be returned instead, for the scope of the
		// inner err variable ends at the end of the
		// outer if-clause.
		if b {
			n = 1
		}
	}
	return n, err
}

func main() {
	fmt.Println(parseInt("TRUE"))
}
The output:
1 strconv.Atoi: parsing "TRUE": invalid syntax

Go only has 25 keywords. Keywords can't be used as identifiers. Many familiar words in Go are not keywords, such as int, bool, string, len, cap, nil, etc. They are just predeclared (built-in) identifiers. These predeclared identifiers are declared in the universe block, so custom declared identifiers can shadow them. Here is a weird example in which many predeclared identifiers are shadowed. Its compiles and runs okay.
package main

import (
	"fmt"
)

// Shadows the built-in function identifier "len".
const len = 3
// Shadows the built-in const identifier "true".
var true = 0
// Shadows the built-in variable identifier "nil".
type nil struct {}
// Shadows the built-in type identifier "int".
func int(){}

func main() {
	fmt.Println("a weird program")
	var output = fmt.Println

	// Shadows the package import "fmt".
	var fmt = [len]nil{{}, {}, {}}
	// Sorry, "len" is a constant.
	// var n = len(fmt)
	// Use the built-in cap function instead, :(
	var n = cap(fmt)

	// The "for" keyword is followed by one
	// implicit local code block and one explicit
	// local code block. The iteration variable
	// "true" shadows the package-level variable
	// "true" declared above.
	for true := 0; true < n; true++ {
		// Shadows the built-in const "false".
		var false = fmt[true]
		// The new declared "true" variable
		// shadows the iteration variable "true".
		var true = true+1
		// Sorry, "fmt" is an array, not a package.
		// fmt.Println(true, false)
		output(true, false)
	}
}
The output:
a weird program
1 {}
2 {}
3 {}

Yes, this example is extreme. It contains many bad practices. Identifier shadowing is useful, but please don't abuse it.


Index↡

The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.

주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.

이 책의 디지털 버전은 아래와 같은 곳을 통해서 구매할 수 있습니다.
Go 101의 저자인 Tapir는 2016년 7월부터 Go 101 시리즈 책들을 집필하고 go101.org 웹사이트를 유지 관리하고 있습니다. 새로운 콘텐츠는 책과 웹사이트에 수시로 추가될 예정입니다. Tapir는 인디 게임 개발자이기도 합니다. Tapir의 게임을 플레이하여 Go 101을 지원할 수도 있습니다. (안드로이드와 아이폰/아이패드용):
  • Color Infection (★★★★★), 140개 이상의 단계로 이루어진 물리 기반의 캐주얼 퍼즐 게임
  • Rectangle Pushers (★★★★★), 2가지 모드와 104개 이상의 단계로 이루어진 캐주얼 퍼즐 게임
  • Let's Play With Particles, 세가지 미니 게임이 있는 캐주얼 액션 게임
페이팔을 통한 개인 기부도 환영합니다.

색인: