Cloud Academy
  1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Introduction to the Go Programming Language

Defer

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students159
Ratings
5/5
star star star star star

Description

If you're thinking about engineering the next big dotcom application then you should seriously consider using Go!! 

The Go Programming Language is without doubt one of the hottest languages to learn, particularly in this cloud native era. More and more companies are adopting Go to engineer highly performant, stable and maintainable applications. Popular projects such as Docker, Kubernetes, Terraform, Etcd, Istio, InfluxDB have all been built successfully using Go!! 

This introductory level training course is designed to bring you quickly up to speed with the many key features that the Go programming language provides. You'll also learn how to setup your own Go development environment - consisting of the Go toolchain, Visual Studio Code, and several related Go based extensions - all to ensure that you are able to be productive writing your own source code.

We’d love to get your feedback on this course, so please give it a rating when you’re finished. If you have any queries or suggestions, please contact us at support@cloudacademy.com.

Learning Objectives

By completing this course, you will:

  • Learn about what makes Go a great language
  • Learn how to install the Go toolchain
  • Learn how to setup Visual Studio Code to edit and debug Go programs
  • Learn how to work with the Go Playground to test and run snippets of Go code
  • Learn and understand the basic Go language syntax and features
  • Learn how to use the Go tool chain commands to compile, test, and manage Go code
  • And finally, you’ll learn how to work with and manage Go modules for module dependency management

Intended Audience

This course is intended for:

  • Anyone interested in learning the Go Programming Language
  • Software Developers interested in using Go to compile and test Go based applications
  • DevOps practitioners looking to learn about Go to support Go based applications

Prerequisites

To get the most from this course, you should have at least:

  • A basic understanding of software development and the software development life cycle

Source Code

All sample Go source code as used and demonstrated within this course can be found here:

Transcript

- [Jeremy Cook] The defer statement is used to defer the execution of a function until the currently running and surrounding function's execution returns. 

A motivation for which you may consider using defer could be when you're working with or processing external resources. For example, files on the file system, or perhaps database connections. For example, you may have a requirement to open a file and manipulator. By calling defer immediately after opening it, you can be assured that it gets closed and written back to the file system. once the surrounding function or method completes, regardless of the execution path taken within it. Calling defer also has the added benefit of insuring that you don't unintentionally leak resources due to unforeseen program flow, since your resource management is explicitly declared, right from the start. 

Using a defer statement ensures that the deferred function is always called, regardless of the execution path within the surrounding function. It's also possible to stack up multiple defer statements and then have them processed in LIFO order, last in, first out. Again, only after the surrounding function returns. Within the example system function seen here, defer is used on lines 12 and 13 to ensure that the do something function is executed immediately after the system function execution completes, with the messages "clean up" and "stop" respectively. 

Now, when this program is executed, the order of the printed items will be "system started", "system finished", "stop", "clean up," and "one." In summary, you have just observed that deferred functions are called after the surrounding function returns, how multiple deferred functions are called in LIFO order, and that deferred functions are useful for cleaning up resources.

About the Author

Students18960
Labs32
Courses79
Learning paths17

Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.

Jeremy holds professional certifications for both the AWS and GCP cloud platforms.
 

Covered Topics