Featured image of post Let's GO 哥布林 - 基本型別

Let's GO 哥布林 - 基本型別

認識 Go 常見的基本型別,以及對應的型別別名。

Go 基本型別

Golang 基本型別包含:

  • bool

  • string

  • int、int8、int16、int32、int64

  • uint、uint8、uint16、uint32、uint64、uintptr (將 pointer 轉換成 int)

  • byte // uint8 的別名 (alias)

  • rune // int32 的別名 (alias) 代表 Unicode code point

  • float32、float64、complex64、complex128

型別後面的大小 (8, 16, 32, 64, 128…) 代表記憶體儲存這個變數,需要佔用多少位元 (bit) 的空間。


型別預設大小

intuint 這類型別的預設大小,則端看程式運行環境而定,可能會是 32 或 64 位元。

uint 型別代表正整數,所以無法儲存負數。

如果沒有特殊效能需求,一般來會用的型別如下:

  • int

  • uint

  • float64

  • complex128


型別別名

由於 Golang 時常用在系統開發專案,所以像 unit8 以及 int32提供了別名,讓變數讀起來含義更明顯。

1
func Read(p []byte) (n int, err error)

比方說看到上面的函式,我們馬上就知道是處理二進位資料。

1
2
3
for _, r := range "Go語言" {
    fmt.Printf("%c = %U\n", r, r) // r is a rune (int32)
}

這個迴圈的 r 則是代表 rune,也就是 Unicode code point

Licensed under CC BY-NC-SA 4.0
使用 Hugo 建立
主題 StackJimmy 設計