- Reference: Go With Example, Effective Go, Go Chinese Docs,Blog
In Go language, Complex structures can be built during initialization and the ordering issues among initialized objects, even among different packages, are handled correctly.
Variable
variable
In Go, variables are explicitly declared and used by the compiler to e.g. check type-correctness of function calls.
Create Time: Runtime
can be any type
initializer can be a general expression computed at run time.
1
2
3
4
5var (
home = os.Getenv("HOME")
user = os.Getenv("USER")
gopath = os.Getenv("GOPATH")
)Multi declare methods:
1
2
3
4
5
6
7
8
9
10
11// declare format: var var_name var_type
var name string
var age int
var isOk bool
// declare multi varablie at once
var (
a string
b int
c bool
d float32
)Multi initialization methods:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package main
import "fmt"
func main() {
// fundamental init method: var var_name var_type = var_value
var x int = 0
// var var_name = var_value :Go will infer the type of initialized variables
var a = "initial"
fmt.Println(a)
// delcare and init multi variable at once
var b, c int = 1, 2
fmt.Println(b, c)
var d = true
fmt.Println(d)
// Variables declared without a corresponding initialization are zero-valued.
var e int
fmt.Println(e)
// The := syntax is shorthand for declaring and initializing a variable
f := "apple"
fmt.Println(f)
}
// initial
// 1 2
// true
// 0
// appleConstant
Create Time: compile time
can only be:
numbers
,characters
,string
,booleans
Define location: could be defined as locals in functions
constant expression: only constant expression
1<<3
: Correctmath.Sin(math.Pi)
: Wrong
Multi init methods:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package main
import (
"fmt"
"math"
)
// A const statement can appear anywhere a var statement can.
const s string = "constant"
func main() {
fmt.Println(s)
// A numeric constant has no type until it’s given one, such as by an explicit conversion.
const n = 500000000
const d = 3e20 / n
fmt.Println(d)
fmt.Println(int64(d))
// A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here math.Sin expects a float64.
fmt.Println(math.Sin(n))
}
// constant
// 6e+11
// 600000000000
// -0.28470407323754404
enumerated constant:
iota
, iota1
2
3
4
5
6
7
8
9
10
11
12
13type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)Initialization
Call Time:After all the variable declarations in the package have evaluated;
a common use of
init
functions is to verify or repair correctness of the program state before real execution begins.1
2
3
4
5
6
7
8
9
10
11
12
13func init() {
if user == "" {
log.Fatal("$USER not set")
}
if home == "" {
home = "/home/" + user
}
if gopath == "" {
gopath = home + "/go"
}
// gopath may be overridden by --gopath flag on command line.
flag.StringVar(&gopath, "gopath", gopath, "override default GOPATH")
}