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.
Program/Source Code:
The source code to demonstrate Sizeof() operator is given below. The given program is compiled and executed successfully.
Output:
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.