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
Although there are many options, for example, simply import unsafe and use sizeof;
import unsafe "unsafe" size := unsafe.Sizeof(bytes)
Note that for some types, such as slices, Sizeof will give you the size of the slice descriptor, which is most likely not the one you want. Also, remember that the length and capacity of the slice are different, and the value returned by the binary .Size reflects the length.
evanmcdonnal
source share