proxytoworld
V2EX  ›  问与答

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

  •  
  •   proxytoworld · Jan 25, 2022 · 1923 views
    This topic created in 1620 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
        1
    hwef  
       Jan 25, 2022
    不是,是类型转换
    proxytoworld
        2
    proxytoworld  
    OP
       Jan 25, 2022
    @hwef 从 string 到 byte 切片的转换?
    mcfog
        3
    mcfog  
       Jan 25, 2022   ❤️ 1
    都对,确实是类型转换,但确实是专门为了 `[]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   ·   Solana   ·   3933 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.