C++ and several other languages have a function called sizeof(int)
(or whatever type you need) that returns the number of bytes consumed by a particular data type, in the current system.
Is there an equivalent function in Go? What is it?
int is one of the available numeric data types in Go . int has a platform-dependent size, as, on a 32-bit system, it holds a 32 bit signed integer, while on a 64-bit system, it holds a 64-bit signed integer.
In Go Strings are UTF-8 encoded, this means each charcter called rune can be of 1 to 4 bytes long. Here,the charcter ♥ is taking 3 bytes, hence the total length of string is 7.
Sizeof() is the correct solution.
If you simply want to find out the size of an int
or uint
, use strconv.IntSize
.
Package strconv
Constants
const IntSize = intSize
IntSize
is the size in bits of anint
oruint
value.
For example,
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
fmt.Println(strconv.IntSize)
}
Output:
gc amd64 linux
64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With