将字节数组转换为int64 golang数组

I came across a situation where I want to convert an array of byte to array of int64 and I am trying to do the below

func covertToInt64(message []byte) []int64{
    rbuf := bytes.NewBuffer(message)
        arr := []int64{}
        e := binary.Read(rbuf, binary.LittleEndian, &arr)
        if e != nil {

        }
    return arr
    }

The above returns an empty arr but when I convert []byte to a string as below

msg:=string(message)

msg have the value "[1,2]"

May I know a better and correct way to do this in Go?

The question is what is exactly that you want? If the message is byte values from 0 to 0xFF, and you simply want to cast each member of the slice into int64, then the answer is:

ints := make([]int64, len(message))
for index, b := range message {
    ints[index] = int64(b)
}

If the the message is the binary data, representing int64 values, then the solution is a bit more complicated than that. Because int64 is 8 bytes long each, thus to be able to convert a slice of bytes, the length of the message must be divisible by eight without any remainder at it's best. We're dropping other cases here.

So, then the answer is:

ml := len(message)
il := ml/8

if ml%8 != 0 {
    // there's more than il*8 bytes, but not 
    // enough to make il+1 int64 values
    // error out here, if needed
}

ints := make([]int64, il)
err := binary.Read(bytes.NewReader(message), ints)

The thing is that when you call binary.Read you need to know the size of the destination value in advance. And the reading fails because: destination length is zero, and in addition the source length is not enough to read even a single one int64 value.

I guess the second situation is a bit more complicated and what you actually wanted can be solved with the first scenario.