Home »
Golang »
Golang Programs
Golang program to demonstrate Sizeof() operator
Here, we are going to demonstrate Sizeof() operator in Golang (Go Language)?
By Nidhi Last updated : March 28, 2023
Sizeof() operator in Golang
In this program, we will use Sizeof() operator to find the size of the specified variable and print the result on the console screen.
Golang code to demonstrate the example of Sizeof() operator
The source code to demonstrate Sizeof() operator is given below. The given program is compiled and executed successfully.
// Golang program to demonstrate Sizeof() operator
package main
import "fmt"
import "unsafe"
func main() {
var num1 int = 10
var num2 byte = 20
fmt.Printf("Size of Num1: %d", unsafe.Sizeof(num1))
fmt.Printf("\nSize of Num2: %d", unsafe.Sizeof(num2))
}
Output
Size of Num1: 8
Size of Num2: 1
Explanation
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created two variables num1, num2 that are initialized with 10, 20 respectively. After that, we used Sizeof() operator to find the number of bytes occupied in memory and print the result on the console screen.
Golang Basic Programs »