If you want to know the size of a certain value, there are two ways to do this - use an unsafe package or use a reflection package. The following code demonstrates both:
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int
fmt.Printf("Size of var (reflect.TypeOf.Size): %d\n", reflect.TypeOf(i).Size())
fmt.Printf("Size of var (unsafe.Sizeof): %d\n", unsafe.Sizeof(i))
}
However, I do not know how to get the type size directly. But I think you will find out that the sizeof function is not needed as often as in C.
rob74 source
share