image
Variadic Functions
Start course
Difficulty
Beginner
Duration
2h 17m
Students
3761
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

- [Jeremy Cook] Functions can also be defined with a variable length trailing or last input parameter. These types of functions are referred to as Variadic Functions. 

It's important to understand that a variadic input parameter must be declared as the last parameter in the function signature. A variadic input parameter will be passed into the function as a slice. Let's see how this works. The displayCount function implemented on line five is declared with two parameters. Notice here that the string data type is preceded by three ellipses. This informs the compiler that the letters input parameter is a variadic input param, it is variable in length. Back within the main function, lines 16 and 17 show you how to call the displayCount function with a variable length set of input parameters. 

The very first parameter is passed to the id input parameter with all remainder strings being wrapped up and passed to the letters variadic input param which becomes a slice of string. The letters slice is iterated over using the range keyword. You can see this on lines seven, eight, and nine. In this case I'm simply implementing a basic count for demonstration purposes. 

Another interesting Go programming technique to consider when calling variadic functions is implemented on lines 19 and 20. Line 19 declares a slice of strings which is then passed into the displayCount function on line 20, where it is first unpacked using the 3 ellipses with the result being the displayCount function being executed in the same manner as previously described. Running this example, produces the following output. 

In summary, you have observed how to declare a variadic function which takes a variable list of input arguments for the last parameter, how to call variadic functions including using a slice and unpacking it and how within a variadic function, the last input parameters type is a slice and how you can use the range keyword to iterate over its contents.

About the Author
Students
132675
Labs
68
Courses
112
Learning Paths
183

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

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

Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).

Covered Topics