Convert array to slice in Go

后端 未结 4 646
盖世英雄少女心
盖世英雄少女心 2020-12-07 18:40

This seems like it would be a fairly common thing and abundant examples across the interwebs, but I can\'t seem to find an example of how to convert an [32]byte

4条回答
  •  心在旅途
    2020-12-07 19:04

    This should work:

    func Foo() [32]byte {
        return [32]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
    }
    
    func Bar(b []byte) {
        fmt.Println(string(b))
    }
    
    func main() {
        x := Foo()
        Bar(x[:])
    }
    

    And it doesn't create a copy of the underlying buffer

提交回复
热议问题