Go語言中的byte和rune

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

Go語言中byterune實質上就是uint8int32類型。byte用來強調資料是raw data,而不是數字;而rune用來表示Unicodecode point。參考規範:

uint8       the set of all unsigned  8-bit integers (0 to 255)int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)byte        alias for uint8rune        alias for int32

可以通過下面程式驗證:

package mainimport "fmt"func byteSlice(b []byte) []byte {    return b}func runeSlice(r []rune) []rune {    return r}func main() {    b := []byte{0, 1}    u8 := []uint8{2, 3}    fmt.Printf("%T %T \n", b, u8)    fmt.Println(byteSlice(b))    fmt.Println(byteSlice(u8))    r := []rune{4, 5}    i32 := []int32{6, 7}    fmt.Printf("%T %T \n", r, i32)    fmt.Println(runeSlice(r))    fmt.Println(runeSlice(i32))}

執行結果如下:

[]uint8 []uint8[0 1][2 3][]int32 []int32[4 5][6 7]

參考資料:
Difference between []uint8 && []byte (Golang Slices)。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.