Go語言學習筆記(二)
阿新 • • 發佈:2018-12-06
6,變數
1: var a int // 預設a=0
2: var b string // b=""3: var c bool // c=Fales
4: var d int = 85: var e string = "hello"6: 或者7: var{8: a int9: b string
10: c bool11: d int = 812: e string = "hello"13: }14: 格式化輸出:15: fmt.Printf("a=%d b=%s c=%t d=%d e=%s\n", a, b, c, d, e)
7,常量
a.常量使用const修飾,代表永遠只是只讀的,不能修改。
b.語法:const identifiler[type]=value,其中type可以省略。
1: const b string = "hello world"2: const b = "hello world"
3: const Pi = 3.14149264: const a = 9/3
或者
1: const{2: b1 string = "hello world"3: b2 = "hello world"
4: Pi = 3.14149265: a = 9/36: }
特殊寫法:
1: const{2: a int = 1003: b4: c int = 2005: d6: }7: a,b=100 c,d=100
iota每多一行就自增加一:
1: const{2: a = iota3: b4: c5: }6: a=0 b=1 c=2
也可以這樣寫,效果和上面一樣:
1: const{2: a = iota3: b = iota4: c = iota5: }
另外一種用法:
1: const{2: a1 = 1 << iota // iota預設等於0,a1左移0位,a1=13: a2 // a2左移1位,10(2)=2(10)
4: a3 // a3左移2位,100(2)=4(10)
5: }6: a1=1 a2=2 a3=4
一個複雜點的例子:
1: const{2: a = iota3: b = iota4: c = iota5: d = 86: e7: f = iota8: g = iota9: }10: //輸出:0,1,2,8,8,5,6