Convert between slices of different types

前端 未结 7 799
南旧
南旧 2020-11-30 01:35

I get a byte slice ([]byte) from a UDP socket and want to treat it as an integer slice ([]int32) without changing the underlying array, and vice ve

相关标签:
7条回答
  • 2020-11-30 02:25

    http://play.golang.org/p/w1m5Cs-ecz

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        s := []interface{}{"foo", "bar", "baz"}
        b := make([]string, len(s))
        for i, v := range s {
            b[i] = v.(string)
        }
        fmt.Println(strings.Join(b, ", "))
    }
    
    0 讨论(0)
提交回复
热议问题