一.標識符、常量、枚舉、變量

關於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)

發佈了226 篇原創文章 · 獲贊 66 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章