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

Functions as Variables

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students1431
Ratings
4.5/5
starstarstarstarstar-half

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] Functions in Go are considered first-class citizens of the language since they can be declared and held in variables and can even be declared as the return parameter of another function. In the example shown here, the extend function on line five is declared to return a function itself. The return to function is anonymously defined within the body of the extend function on lines seven through to 10. Within the main function on line 16, the extend function is called with the ca string variable which contains the string cloudacademy. The extend function returns a function which is assigned to the variable word. 

A for loop is then used to repeatedly call and invoke the function currently stored in the word variable. The outcome of running this application results in a list of printed statements starting with just the character c, followed by the character sequence c-l on a new line, then followed by the character sequence c-l-o on a new line, and so on until the full string cloudacademy is printed out. 

The anonymous function returned by the extend function is an example of a closure. A closure is a special type of anonymous function that has the ability to reference variables declared outside of itself. In this case, the inner returned function is referencing the variable i designed to be a counter. Now, this is quite similar to how functions can access and potentially manipulate global variables which is strongly argued is not good practice. 

To address this, closures have one other defining ability and that is they can continue to reference variables that they had access to during creation even if those variables are no longer referenced elsewhere. In other words, a closure gives you access to an outer function's scope from within an inner function. This again is demonstrated by the variable i, declared on line six within the extend function. It holds state and is referenced and incremented within that inner returned function. 

In summary, you have observed the following: that functions are first-class citizens within the Go programming language, how to assign anonymous functions to variables, how to create functions that return other functions, and how to use anonymous functions to create closures.

About the Author
Students41378
Labs34
Courses94
Learning paths28

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