2.Variables
# Identifier and Keyword
# Identifier
can be formed by letter, number and _(low dash), and only can start with letter or _
such as:
abc
_
_123
abc123
camelCase is suggested to use in
Go
such as:var studentName string
1
# Keywords
go language has 25 keywords:
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
2
3
4
5
and there are 37 reserved words:
Constants: true false iota nil
Types: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex64 complex128
bool byte rune string error
Functions: make len cap new append copy close delete
complex real imag
panic recover
2
3
4
5
6
7
8
# Variables
# What is variable
The official definition: in computer programming, a variable is a storage location paired with an associated symbolic name (an identifier) which contains a value.
In other words, a variable is a storage location for data. Variables have names. Some computer language mandate you assign a data type to variables. When asked for the "official definition" (or best definition) please use the definition stated above.
# Variable in go
every variable in Go
has its own type, and must determine before use
- you must use the variable after determine it
- can not determine the same variable name in the same scope
# Determine single
variable
the standard format to determine a variable
var VARIABLE_NAME VARIABLE_TYPE
such as:
var name string
var age int
var isOk bool
2
3
# Determine multiple
variable
go
support determine multiple variable at the same time:
var (
a string
b int
c bool
d float32
)
2
3
4
5
6
# Variable initialization
when determine variable in go
, it will initialize the variable automatic by default. Each variable will be determined to the default value of its type, such as
int
:0
string
:''
bool
:false
slice
,func
:nil
the standard initialization format as following:
var VARIABLE_NAME VARIABLE_TYPE = EXPRESSION
for example:
var name string = 'Alex Zhou'
var age int = 23
2
# Determine and initializing at the same time
var s1 string = "Alex Zhou" // not suggest to use this
# Type Derivation
Go
will set the type for variable according to the type of initial data
var s2 = "Alex"
# Short variable declaration
only able to use inside of the function
s3 := "Acadia University"
# Anonymous variable
when assigning multi variable, us anonymous variable
to ignore useless variable.
anonymous variable
is -
Example:
func foo()(int, string){
return 10, "abcde"
}
func main(){
x,_ := foo()
_,y := foo()
}
2
3
4
5
6
7
# Demo program
package main
import "fmt"
var(
name string
age int
isHungry bool
)
func foo()(int, string){
return 10, "abcde"
}
func main(){
name = "Alex Zhou"
age = 23
isHungry = true
// unglobal variable in Go lan must be used, or the compiler will give error
fmt.Printf("%s is %d years old, if he is hungry: %t\n", name, age, isHungry)
fmt.Print("Age: ", age)
fmt.Println("Name: ", name)
// Determine and initializing at the same time
var s1 string = "Alex Zhou" // not suggest to use this
fmt.Println(s1)
// Type Derivation
var s2 = "Alex"
fmt.Println(s2)
// Short variable declaration
// only able to use inside of the function
s3 := "Acadia University"
fmt.Println(s3)
x,_ := foo()
fmt.Println(x)
_,y := foo()
fmt.Println(y)
}
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Constant
# const
const
usually use for global, constant must initialize when determine it, such as:
const pi = 3.1415926
const e = 2.7182
2
after determine pi
and e
they will never change during the running of the program
you can determine multi const
together:
const(
pi = 3.1415926
e = 2.7182
)
const(
d1, d2 = 1, 2
d3, d4 = 3, 4
)
2
3
4
5
6
7
8
9
if const
doesn't have value, it means this const
has the same value as above:
const (
n1 = 100
n2
n3
)
2
3
4
5
# iota
iota
is a counter of const
, only be able to use inside the const
determination.
iota
will reset to 0
when const
show up, each line in the const
determination will increase the iota
by 1
. Such as:
const (
n1 = iota // 0
n2 // 1
n3 // 2
n4 // 3
)
const (
c1 = iota // 0
c2 = 100
c3 = iota // 2
c4 // 3
)
2
3
4
5
6
7
8
9
10
11
12
13
# iota skip value
const (
b1 = iota // 0
b2 //1
_
b3 // 3
)
2
3
4
5
6
# use case: Define order of magnitude
const (
_ = iota
KB = 1 << (10 * iota)
MB = 1 << (10 * iota)
GB = 1 << (10 * iota)
TB = 1 << (10 * iota)
PB = 1 << (10 * iota)
)
2
3
4
5
6
7
8