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

Functions and Anonymous Functions

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students474
Ratings
4.6/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

- Go functions are declared using the func keyword. You may have noticed the main function in all of the previous coding examples presented this as a good example of declaring a function. 

Declaring a function allows you to create a reusable block of code, which is typically parameterised with one or several input parameters, and may or may not return one or many values. Function input parameters and return types are optional. A function can heavily be declared without any input and output. In fact, the special main function that is used to start an executable application is a perfect example of this type of function. 

Functions are most often declared with package scope, but they can be declared and embedded within other functions, and when they are, they have function scope only. Function naming is also important and can contribute to the visibility of the function. If a functions name begins with an uppercase letter, then the function is considered public and can be called from outside of the package. If, instead, a functions name begins with a lowercase letter, then it can only be called from within the current package file. In the example provided here, the sum and minus functions declared at the top and just beneath the input statement can be called from anywhere within the same file only since both their names begin with a lowercase letter. 

Both functions take two input parameters. num1 and num2. And are both typed as integers. The sum function returns a single int value, which is the result of adding num1 and num2 together. Likewise, the minus function returns a single named int value result, which is the result of subtracting num2 from num1. Notice here that the enclosed return statement is called by itself. This is possible since we are declaring a named returned variable in the functions signature. Also note on line 10 that since all input parameters are of the same int type, then we only need to type them once. Moving further down the example, an anonymous function is declared on line 19 inside the main function. This demonstrates a couple of interesting things about functions. First up is that functions can be declared within other functions. Next is that a function can be defined without specifying a name. In this case, they are considered anonymous. This example immediately calls the anonymous function with the input values five and three enclosed in parentheses on line 23. 

I'll explore anonymous functions and higher-order functions in more detail later on. Running this example produces the following output. 

In summary, you have just observed how to declare private functions with package scope, how to create a function signature, including defining input parameters and return types, how to specify named return types, and how to create an anonymous function within another function and call it immediately.

About the Author

Students26960
Labs32
Courses93
Learning paths22

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