Go 邊看邊練 -《Go 學習筆記》系列(二)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

上一篇: Go 邊看邊練 -《Go 學習筆記》系列(一)

1.3 基本類型

更明確的數字類型命名,支援 Unicode,支援常用資料結構。

類型 長度 預設值 說明
bool 1 false
byte 1 0 uint8
rune 4 0 Unicode Code Point, int32
int, uint 4 或 8 0 32 或 64 位元
int8, uint8 1 0 -128 ~ 127, 0 ~ 255
int16, uint16 2 0 -32768 ~ 32767, 0 ~ 65535
int32, uint32 4 0 -21億 ~ 21 億, 0 ~ 42 億
int64, uint64 8 0
float32 4 0.0
float64 8 0.0
complex64 8
complex128 16
uintptr 4 或 8 足以儲存指標的 uint32 或 uint64 整數
array 實值型別
struct 實值型別
string "" UTF-8 字串
slice nil 參考型別
map nil 參考型別
channel nil 參考型別
interface nil 介面
function nil 函數

支援八進位、十六進位,以及科學記號標記法。標準庫 math 定義了各數字類型取值範圍。

a, b, c, d := 071, 0x1F, 1e9, math.MinInt16

null 指標值 nil,而非 C/C++ NULL。

1.4 參考型別

參考型別包括 slicemapchannel。它們有複雜的內部結構,除了申請記憶體外,還需要初始化相關屬性。

內建函數 new 計算類型大小,為其分配零值記憶體,返回指標。而 make 會被編譯器翻譯成具體的建立函數,由其分配記憶體和初始化成員結構,返回對象而非指標。

a := []int{0, 0, 0} // 提供初始設定式。a[1] = 10b := make([]int, 3) // makesliceb[1] = 10c := new([]int)c[1] = 10 // Error: invalid operation: c[1] (index of type *[]int)

有關參考型別具體的記憶體布局,可參考後續章節。

1.5 類型轉換

不支援隱式類型轉換,即便是從窄向寬轉換也不行。

var b byte = 100// var n int = b // Error: cannot use b (type byte) as type int in assignmentvar n int = int(b) // 顯式轉換

使用括弧避免優先順序錯誤。

*Point(p) // 相當於 *(Point(p))(*Point)(p)<-chan int(c) // 相當於 <-(chan int(c))(<-chan int)(c)

同樣不能將其他類型當 bool 值使用。

a := 100if a { // Error: non-bool a (type int) used as if condition    println("true")}

1.6 字串

字串是不可變實值型別,內部用指標指向 UTF-8 位元組數組。

  • 預設值是Null 字元串 ""。
  • 用索引號訪問某位元組,如 s[i]
  • 不能用序號擷取位元組元素指標,&s[i] 非法。
  • 不可變類型,無法修改位元組數組。
  • 位元組數組尾部不包含 NULL

runtime.h

struct String{    byte* str;    intgo len;};

使用索引號訪問字元 (byte)。

s := "abc"println(s[0] == '\x61', s[1] == 'b', s[2] == 0x63)

輸出:

true true true

使用 "`" 定義不做轉義處理的原始字串,支援跨行。

s := `ab\r\n\x00c`println(s)

輸出:

ab\r\n\x00c

串連跨行字串時,"+" 必須在上一行末尾,否則導致編譯錯誤。

s := "Hello, " +"World!"s2 := "Hello, "+ "World!" // Error: invalid operation: + untyped string

支援用兩個索引號返回子串。子串依然指向原位元組數組,僅修改了指標和長度屬性。

s := "Hello, World!"s1 := s[:5] // Hellos2 := s[7:] // World!s3 := s[1:5] // ello

單引號字元常量表示 Unicode Code Point,支援 \uFFFF\U7FFFFFFF\xFF 格式。對應 rune 類型,UCS-4。

要修改字串,可先將其轉換成 []rune[]byte,完成後再轉換為 string。無論哪種轉換,都會重新分配記憶體,並複製位元組數組。

for 迴圈遍曆字串時,也有 byterune 兩種方式。

下一篇: Go 邊看邊練 -《Go 學習筆記》系列(三)

  • 本系列是基於雨痕《Go 學習筆記》(第四版)整理彙編而成,非常感謝雨痕的辛勤付出與分享!
  • 轉載請註明:文章轉載自:駭客與畫家的社區 [http://symphony.b3log.org]
  • 如果你覺得本章節做得不錯,請在下面打賞一下吧~

社區小貼士

  • 關註標簽 golang 可以方便查看 Go 相關文章
  • 關註標簽 Go學習筆記 可以方便查看本系列
  • 關注作者後如有新帖將會收到通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.