关于GoLand环境的配置,比较简单,直接到官网下载下来解压就可以使用,具体的可以参考我之前的博客:http://blog.csdn.net/linuxandroidwince/article/details/75529376
标识符
与C,C++,JAVA等语言类似,支持字母、数字与下划线命名的标识符。所有标识符区分大小写,比如Xyzabc, xYzAbc, xyzABC是三个不同的标识符。
关键字
break default func interface select
case defer go map struct
const else goto package switch
chan fallthrough if range type
continue for import return var
预定义标识符
append copy int8 nil true
bool delete int16 Panic unit
byte error int32 print uint8
cap false int64 println uint16
close float32 iota real uint32
complex float64 len recover uint64
complex64 imag make rune uintptr
complex128 int new string
空标识符
下划线’_’表示一个空标识符,它的作用仅是一个占位符,通常用在赋值操作中,从而丢弃该值的目的。
注意,空标识符不是一个变量,所以在:=这样的赋值中一定要至少指明另一个具名变量。栗子:_, cout := foo(x)
常量、枚举和变量
常量、枚举
常量定义用关键字const声明:
const a = 0
const b = 1
const c = 2
上述书定风格有点类似C,C++,但如果有多个常量定义时,在Go语言下面可以这样定义:
const (
a = 0
b = 1
c = 2
)
上述这种定义也叫枚举,在Go语言里有一个预定义标识符,专门为常量定义所用,特别是枚举定义中,能简化定义,这个标识符是iota。
iota是golang语言的常量计数器,只能在常量的表达式中使用。
iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(iota可理解为const语句块中的行索引)。
- iota只能在常量的表达式中使用。
fmt.Println(iota)
编译时会报错:
src/github.com/user/test/test.go:12: undefined: iota
- 自增长常量
将上面的枚举定义做下修改:
const (
a = iota
b
c
)
测试:
package main
import "fmt"
func main() {
const (
a = iota
b
c
)
fmt.Println("iota test: ", a, b, c)
}
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 0 1 2
- 每次 const 出现时,都会让 iota 初始化为0
package main
import "fmt"
func main() {
const d = iota
fmt.Println(d)
const (
a = iota
b
c
)
fmt.Println("iota test: ", a, b, c)
}
jack@jxes-VirtualBox:~/gopath$ make -f Makefile-boo
go install github.com/user/test
jack@jxes-VirtualBox:~/gopath$ ./bin/test
0
iota test: 0 1 2
- iota与空标识符’_’
可以跳过中间的值
package main
import "fmt"
func main() {
const (
a = iota
b
c
_
_
d
e
)
fmt.Println("iota test: ", a, b, c, d, e)
}
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 0 1 2 5 6 //中间3,4被跳过
- 位掩码表达式中的使用
package main
import "fmt"
func main() {
const (
a = 1 << iota
b
c
)
fmt.Println("iota test: ", a, b, c)
}
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 1 2 4
在 Go 语言的 spec 中, 这就是所谓的隐性重复最后一个非空的表达式列表。
- 多常量定义在一行的神奇表现
package main
import "fmt"
func main() {
const (
a, b = iota + 1, iota + 2
c, d
e, f
)
fmt.Println("iota test: ", a, b, c, d, e, f)
}
jack@jxes-VirtualBox:~/gopath$ make -f Makefile-boo
go install github.com/user/test
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 1 2 2 3 3 4
iota 在下一行增长,而不是立即取得它的引用,再来看下面这个栗子:
1 package main
2
3 import "fmt"
4
5 func main() {
6 const (
7 a, b, c = iota + 1, iota + 2, iota + 3
8 d, e, f
9 g, h, i
10 )
11 fmt.Println("iota test: ", a, b, c, d, e, f, g, h, i)
12 }
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 1 2 3 2 3 4 3 4 5
通过上面这两个栗子,也可以看出隐藏重复表达式的特性。另外,第一行定义几个常量,比如a,b,c三个,那么下一行一定也是定义3个,少或多编译时都会报错,这也体现了隐藏重复的特性。
下面是下一行常量个数不一致情形编译时报错:
jack@jxes-VirtualBox:~/gopath$ make -f Makefile-boo
go install github.com/user/test
# github.com/user/test
src/github.com/user/test/test.go:8: missing value in const declaration
src/github.com/user/test/test.go:9: missing value in const declaration
src/github.com/user/test/test.go:11: undefined: e
src/github.com/user/test/test.go:11: undefined: h
Makefile-boo:6: recipe for target 'all' failed
make: *** [all] Error 2
- 中间插队的两种情形
1 package main
2
3 import "fmt"
4
5 func main() {
6 const (
7 a = iota
8 b
9 c = 201
10 d
11 e
12 )
13 fmt.Println("iota test: ", a, b, c, d, e)
14 }
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 0 1 201 201 201
这里d,e重复前面的c,将上述代码稍作修改:
1 package main
2
3 import "fmt"
4
5 func main() {
6 const (
7 a = iota
8 b
9 c = 201
10 d = iota
11 e
12 )
13 fmt.Println("iota test: ", a, b, c, d, e)
14 }
jack@jxes-VirtualBox:~/gopath$ ./bin/test
iota test: 0 1 201 3 4
这两种写法,其实也是对Go语言中隐藏重复表达式的体现,具体情况要具体使用。
变量
变里定义有以下几种定义:
var varname type // var i int
varname := value // i := 100
varname := type(value) // i := float64(9838475845)