proxytoworld
V2EX  ›  问与答

go byte 切片初始化是有语法糖嘛

  •  
  •   proxytoworld · Jan 25, 2022 · 2241 views
    This topic created in 1745 days ago, the information mentioned may be changed or developed.

    在学习 io 接口的时候,看到这样的初始化代码

    []byte("hello ")
    
    一般切片初始化为:
    []int{1,2,3,4}
    []string{"aaa","bbbb"}
    

    byte 切片这种方式是语法糖?

    3 replies  •  2022-01-25 18:08:36 +08:00
    hwef
    hwef
    Jan 25, 2022
    1
    不是,是类型转换
    proxytoworld
    proxytoworld
    OP
    Jan 25, 2022
    2
    @hwef 从 string 到 byte 切片的转换?
    mcfog
    mcfog
    Jan 25, 2022❤️1
    3
    都对,确实是类型转换,但确实是专门为了 `[]byte` ( 和 `[]rune` ) 做的特例
    https://go.dev/ref/spec#:~:text=x%20is%20a%20string%20and%20T%20is%20a%20slice%20of%20bytes%20or%20runes.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1564 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:38 · PVG 07:38 · LAX 15:38 · JFK 18:38
    ♥ Do have faith in what you're doing.