avatarRichard Bell

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

889

Abstract

">()</span> { fmt.Println(<span class="hljs-string">"2"</span>) }</pre></div><div id="f7d3"><pre><span class="hljs-variable">func</span> <span class="hljs-function"><span class="hljs-title">main</span>() { <span class="hljs-variable">defer</span> <span class="hljs-title">two</span>()</span> <span class="hljs-function"><span class="hljs-title">one</span>()</span> }</pre></div><p id="76fb">Calling the main function we would see the following output:</p><div id="01a9"><pre>1 2</pre></div><p id="f5ff">It would be the same as calling the functions in this order:</p><div id="5f65"><pre><span class="hljs-variable">func</span> <span class="hljs-function"><span class="hljs-title">main</span>() { <span class="hljs-title">one</span>()</span> <span class="hljs-function"><span class="hljs-title">two</span>()</span> }</pre></div><p id="08db">Seems a little pointless in this exampl

Options

e, however it is very useful for clean up tasks as it will run even if an error occurs, or something is returned. You may see something like this:</p><div id="6cb2"><pre><span class="hljs-attribute">db</span> <span class="hljs-operator">=</span> CreateDBConnection() defer db.CloseConnection()</pre></div><p id="7b42">Followed by some useful operations on the database. Another advantage here is that it keeps our close call close to the create call so it’s easier to see that it’s being handled.</p><p id="e5e5">More <a href="https://richard-t-bell90.medium.com/list/golang-in-sixty-seconds-7a26c5131734">Golang in sixty seconds</a></p><p id="23a4"><a href="https://richard-t-bell90.medium.com/membership"><i>Get Unlimited access to Medium</i></a></p><p id="88ce"><a href="https://ko-fi.com/richardtbell"><i>Buy me a coffee</i></a><i> if you enjoyed the article :)</i></p></article></body>

Golang in sixty seconds — defer

Photo by Karim MANJRA on Unsplash

Golang has a special statement called defer which causes the function to be run after the function completes. Let’s look at an example:

func one() {
  fmt.Println("1")
}
func two() {
  fmt.Println("2")
}
func main() {
  defer two()
  one()
}

Calling the main function we would see the following output:

1
2

It would be the same as calling the functions in this order:

func main() {
  one()
  two()
}

Seems a little pointless in this example, however it is very useful for clean up tasks as it will run even if an error occurs, or something is returned. You may see something like this:

db = CreateDBConnection()
defer db.CloseConnection()

Followed by some useful operations on the database. Another advantage here is that it keeps our close call close to the create call so it’s easier to see that it’s being handled.

More Golang in sixty seconds

Get Unlimited access to Medium

Buy me a coffee if you enjoyed the article :)

Golang
Learning To Code
Softare Development
Programming
Development
Recommended from ReadMedium