9

I am writing a generic function to get the size of any type of structure, similar to sizeof function in C.

I am trying to do this using interfaces and reflection but I'm 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 returns wrong result as 12. I am very new to Go, kindly help me on this.

2 Answers 2

13

You're getting the size of the reflect.Value struct, not of the object contained in the interface T. Fortunately, reflect.Type has a Size() method:

size := reflect.TypeOf(T).Size()

This gives me 40, which makes sense because of padding.

2
  • 4
    Alternatively, you can use unsafe.Sizeof(myType{}).
    – thwd
    Jul 10, 2015 at 12:30
  • 1
    @thwd You are right but my getSize function have to find size of any structure, it has to find the type of structure during run time. Because of that I chose reflect package
    – sujin
    Jul 11, 2015 at 4:14
2

Go 1.18

With Go 1.18 you can use a generic function with unsafe.Sizeof:

func getSize[T any]() uintptr {
    var v T
    return unsafe.Sizeof(v)
}

Note that this will be more performant than using reflect, but it will introduce unsafe in your code base — some static analysis tools may give warnings about that.

However if your goal is to improve code reuse or get sizes at run time (read on for the solution to that), this won't help much because you still need to call the function with proper instantiation:

type myType struct {
    a int
    b int64
    c float32
    d float64
    e float64
}

func main() {
    fmt.Println(getSize[myType]())
}

You might get the most out of this when used as part of some other generic code, e.g. a generic type or function where you pass a type param into getSize. Although if you have the argument v this is equivalent to calling unsafe.Sizeof(v) directly. Using a function could be still useful to hide usage of unsafe. A trivial example:

func printSize[T any](v T) {
    // (doing something with v)
    // instantiate with T and call
    s := getSize[T]() 
    // s := unsafe.Sizeof(v)
    
    fmt.Println(s)
}

Otherwise you can pass an actual argument to getSize. Then type inference will make it unnecessary to specify the type param. This code perhaps is more flexible and allows you to pass arbitrary arguments at runtime, while keeping the benefits of avoiding reflection:

func getSize[T any](v T) uintptr {
    return unsafe.Sizeof(v)
}

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}
    
    // inferred type params
    fmt.Println(getSize(info))       // 40
    fmt.Println(getSize(5.0))        // 8
    fmt.Println(getSize([]string{})) // 24
    fmt.Println(getSize(struct {
        id uint64
        s  *string
    }{}))                            // 16
}

Playground: https://go.dev/play/p/kfhqYHUwB2S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.