Skip to content

Instantly share code, notes, and snippets.

@hachibu
Created September 30, 2022 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachibu/030523b950cec7f706dcb796e85712a2 to your computer and use it in GitHub Desktop.
Save hachibu/030523b950cec7f706dcb796e85712a2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"sort"
"text/tabwriter"
"unsafe"
)
func main() {
sizes := map[string]uintptr{
"bool": unsafe.Sizeof(true),
"complex128": unsafe.Sizeof(complex128(0)),
"complex64": unsafe.Sizeof(complex64(0)),
"float32": unsafe.Sizeof(float32(0)),
"float64": unsafe.Sizeof(float64(0)),
"int16": unsafe.Sizeof(int16(0)),
"int32": unsafe.Sizeof(int32(0)),
"int64": unsafe.Sizeof(int64(0)),
"int8": unsafe.Sizeof(int8(0)),
"string": unsafe.Sizeof(""),
"uint16": unsafe.Sizeof(uint16(0)),
"uint32": unsafe.Sizeof(uint32(0)),
"uint64": unsafe.Sizeof(uint64(0)),
"uint8": unsafe.Sizeof(uint8(0)),
}
keys := make([]string, 0, len(sizes))
for k := range sizes {
keys = append(keys, k)
}
sort.Strings(keys)
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintln(w, "|", "data type", "\t|", "size (bytes)", "\t|")
fmt.Fprintln(w, "|", "---", "\t|", "---", "\t|")
for _, k := range keys {
if v, ok := sizes[k]; ok {
fmt.Fprintln(w, "|", k, "\t|", v, "\t|")
}
}
w.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment