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

后端 未结 3 1395
[愿得一人]
[愿得一人] 2021-02-15 03:16

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:45

    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)
    }
    

提交回复
热议问题