I am writing a generic function which get any type of structure and return size of that structure, similar to sizeof function in C language.
I am trying to do this using interface and reflection but i not able to get the correct result. Code is below
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
type myType struct {
a int
b int64
c float32
d float64
e float64
}
info := myType{1, 2, 3.0, 4.0, 5.0}
getSize(info)
}
func getSize(T interface{}) {
v := reflect.ValueOf(T)
const size = unsafe.Sizeof(v)
fmt.Println(size)
}
This code return wrong result as 12. I am very new to golang, kindly help me on this.