Logo

Programming-Idioms

History of Idiom 109 > diff from v3 to v4

Edit summary for version 4 by :
[Go] Comment: about reference fields

Version 3

2015-12-29, 21:03:22

Version 4

2016-01-03, 23:27:17

Idiom #109 Number of bytes of a type

Set n to the number of bytes of a variable of type T.
This makes different sense (if any), depending on the language.

Idiom #109 Number of bytes of a type

Set n to the number of bytes of a variable of type T.
This makes different sense (if any), depending on the language.

Imports
import "reflect"
Imports
import "reflect"
Code
var t T
tType := reflect.TypeOf(t)
n := tType.Size()
Code
var t T
tType := reflect.TypeOf(t)
n := tType.Size()
Comments bubble
This run-time reflection works on a value of the type T.
Warning: for a given program, the size of a type is not the same on a 32-bit machine or a 64-bit machine.
Comments bubble
This run-time reflection works on a value of the type T.
Note that the size does not include the memory indirectly taken by the reference fields: Strings, slices, etc.
Warning: for a given program, the size of a type is not the same on a 32-bit machine or a 64-bit machine.
Doc URL
https://golang.org/pkg/reflect/#Type
Doc URL
https://golang.org/pkg/reflect/#Type
Demo URL
http://play.golang.org/p/urT_arJfKk
Demo URL
http://play.golang.org/p/urT_arJfKk