Welcome To Golang By Example

Evaluation of defer arguments in Go (Golang)

Table of Contents

Overview

defer arguments are evaluated at the time defer statement is evaluated

Let’s see a program for that

Example

package main

import "fmt"

func main() {
	sample := "abc"

	defer fmt.Printf("In defer sample is: %s\n", sample)
	sample = "xyz"
}

Output

In defer sample is: abc

In the above program when the defer statement was evaluated the value of the sample variable was “abc”. In the defer function, we print the  sample variable. After the defer statement we change the value of the sample variable to “xyz”.  But the program outputs “abc” instead of “xyz” because when the defer arguments were evaluated the value of the  sample variable was “abc”.