DEV Community

Bryan Sazon
Bryan Sazon

Posted on

2 2

Golang: Defer Gotchas and Hack

#go

Sample Program

package main

import "fmt"

func main() {
    p := person{"john", 15}
    defer printPerson(p)
    updateAge(&p, 20)
}

type person struct {
    name string
    age  int
}

func printPerson(p person) {
    fmt.Printf("Name: %s, Age: %d", p.name, p.age)
}

func updateAge(p *person, newAge int) {
    p.age = newAge
}

Run the program.

$ go run main.go

Name: john, Age: 15

I was expecting 20, but I got 15. This is because "The arguments to the deferred function (which include the receiver if the function is a method) are evaluated when the defer executes, not when the call executes.". See effective go - defer.

This means that the time that defer printPerson(p) was called, that p is already set in stone.

Workaround

Now, put printPerson(p) under an anonymous function.


func main() {
    p := person{"john", 15}
    // defer printPerson(p)
    defer func() {
        printPerson(p)
    }()
    updateAge(&p, 20)
}

Run the program.

$ go run main.go

Name: john, Age: 20

Why? The documentation only says the arguments to the deferred function are evaluated when the defer executes. In the modified code above, that deferred function is the anonymous function, and not the function printPerson. Hack!

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay