Golang常量iota的使用實(shí)例
Codes
package main
import "fmt"
type color byte
const (
black color = iota
red
blue
)
func test(c color) {
fmt.Println(c)
}
func main() {
const (
x = iota // 0
y // 1
z // 2
)
fmt.Printf("x=%v, y=%v, z=%v\n", x, y, z)
const (
_ = iota
KB = 1 << (10 * iota) // 1 << (10 * 1)
MB // 1 << (10 * 2)
GB // 1 << (10 * 3)
)
fmt.Printf("KB=%v, MB=%v, GB=%v\n", KB, MB, GB)
const (
_, _ = iota, iota * 10 // 0, 0 * 10
aa, bb // 1, 1 * 10
cc, dd // 2, 2 * 10
)
fmt.Printf("aa=%v, bb=%v, cc=%v, dd=%v\n", aa, bb, cc, dd)
const (
a = iota // 0
b // 1
c = 100 // 100
d // 100
e = iota // 4
f // 5
)
fmt.Printf("a=%v, b=%v, c=%v, d=%v, e=%v, f=%v\n", a, b, c, d, e, f)
const (
g = iota // 0
h float32 = iota // 1
i = iota // 2
)
fmt.Printf("g: %T %v, f: %T %v, h: %T %v\n", g, g, h, h, i, i)
test(black) // 0
test(red) // 1
test(blue) // 2
test(100) // 100 并未超出 color/byte 類型取值范圍
// xx := 2
// test(xx)
}Result
x=0, y=1, z=2
KB=1024, MB=1048576, GB=1073741824
aa=1, bb=10, cc=2, dd=20
a=0, b=1, c=100, d=100, e=4, f=5
g: int 0, f: float32 1, h: int 2
0
1
2
100
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
GoLand一鍵上傳項(xiàng)目到遠(yuǎn)程服務(wù)器的方法步驟
我們開發(fā)項(xiàng)目常常將項(xiàng)目上傳到linux遠(yuǎn)程服務(wù)器上來運(yùn)行,本文主要介紹了GoLand一鍵上傳項(xiàng)目到遠(yuǎn)程服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Golang實(shí)現(xiàn)對(duì)map的并發(fā)讀寫的方法示例
這篇文章主要介紹了Golang實(shí)現(xiàn)對(duì)map的并發(fā)讀寫的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
Go結(jié)構(gòu)體指針引發(fā)的值傳遞思考分析
這篇文章主要為大家介紹了Go結(jié)構(gòu)體指針引發(fā)的值傳遞思考分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

