Q:

Golang program to demonstrate the \'defer\' keyword

belongs to collection: Golang Basic Programs

0

In this program, we will use the defer keyword with Println() function. The defer function is used to put the statement into stack then statements execute in LIFO (Last In First Out) order.

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 the defer keyword is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate
// the "defer" keyword

package main

import "fmt"

func main() {
	defer fmt.Println("Hello")
	defer fmt.Println("Hiiii")
	fmt.Println("Good morning")
}

Output:

Good morning
Hiiii
Hello

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 used the defer keyword and execute the statements in LIFO order. The defer statement is always used with a function call.

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 Sizeof() operator...