More idiomatic way in Go to encode a []byte slice int an int64?

后端 未结 3 2051
借酒劲吻你
借酒劲吻你 2021-02-15 02:50

Is there a better or more idiomatic way in Go to encode a []byte slice into an int64?

package main

import \"fmt\"

func main() {
    var mySlice = []byte{244, 2         


        
3条回答
  •  不知归路
    2021-02-15 03:59

    You can use encoding/binary's ByteOrder to do this for 16, 32, 64 bit types

    Play

    package main
    
    import "fmt"
    import "encoding/binary"
    
    func main() {
        var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
        data := binary.BigEndian.Uint64(mySlice)
        fmt.Println(data)
    }
    

提交回复
热议问题