一.标识符、常量、枚举、变量

关于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语言的常量计数器,只能在常量的表达式中使用。

iotaconst关键字出现时将被重置为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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JXES智能生态系统

如文章对你有用,请作者喝个咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值