字节数组golang的大小

I have a []byte object and I want to get the size of it in bytes. Is there an equivalent to C's sizeof() in golang? If not, Can you suggest other ways to get the same?

I think your best bet would be;

package main

import "fmt"
import "encoding/binary"

func main() {
    thousandBytes := make([]byte, 1000)
    tenBytes := make([]byte, 10)
    fmt.Println(binary.Size(tenBytes))
    fmt.Println(binary.Size(thousandBytes))
}

https://play.golang.org/p/HhJif66VwY

Though there are many options, like just importing unsafe and using sizeof;

import unsafe "unsafe"

size := unsafe.Sizeof(bytes)

Note that for some types, like slices, Sizeof is going to give you the size of the slice descriptor which is likely not what you want. Also, bear in mind the length and capacity of the slice are different and the value returned by binary.Size reflects the length.

To return the number of bytes in a byte slice use the len function:

bs := make([]byte, 1000)
sz := len(bs)
// sz == 1000

If you mean the number of bytes in the underlying array use cap instead:

bs := make([]byte, 1000, 2000)
sz := cap(bs)
// sz == 2000

A byte is guaranteed to be one byte: https://golang.org/ref/spec#Size_and_alignment_guarantees.