Skip to content

Instantly share code, notes, and snippets.

@assyrianic
Last active June 14, 2021 17:13
Show Gist options
  • Save assyrianic/3b8ede42e0d250c2870051faf17f7585 to your computer and use it in GitHub Desktop.
Save assyrianic/3b8ede42e0d250c2870051faf17f7585 to your computer and use it in GitHub Desktop.
getting the type size of an interface
package main
import (
"fmt"
"unsafe"
)
type (
any interface{}
ptr unsafe.Pointer
goIface struct {
_type, _data ptr
}
)
func ptr_size() uintptr {
return unsafe.Sizeof(ptr(nil))
}
func print_type(n any) {
p := ( *goIface )(ptr(&n))
offs := uintptr(0) //ptr_size()
itable := *( *uint )( ptr(uintptr(p._type) + offs) )
fmt.Printf("%+v\n", itable)
}
func main() {
print_type(int(1)) /// 8
print_type(uint(1)) /// 8
print_type(int8(0)) /// 1
print_type(int16(0)) /// 2
print_type(int32(0)) /// 4
print_type(int64(0)) /// 8
print_type(uintptr(0)) /// 8
print_type(float32(0.0)) /// 4
print_type(float64(0.0)) /// 8
print_type("lololol") /// 16
print_type([]int{}) /// 24
print_type([9]int{}) /// 80
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment