Q:

Golang program to demonstrate Sizeof() operator

belongs to collection: Golang Basic Programs

0

In this program, we will use Sizeof() operator to find the size of the specified variable and print the result on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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 num1num2 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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang program to demonstrate the \'defer\�... >>
<< Golang program to demonstrate the \'const\�...