How to Get Memory Size of the Variable in Go

Created
Modified

Using Sizeof Function

You can use the unsafe.Sizeof function for this. It returns the size in bytes.

See the following example:

package main

import (
  "fmt"
  "unsafe"
)

func main() {

  a := 10
  s := "Hello World"

  fmt.Printf("%d\n", unsafe.Sizeof(a))
  fmt.Printf("%d, len: %d\n", unsafe.Sizeof(s), len(s))
}
8
16, len: 11

Related Tags

#get# #memory#