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

Interfaces

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students591
Ratings
4.7/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] An interface in Go is a collection of one or more method signatures that collectively describe the behavior of a type that implements it. By their very definition, interfaces are to be considered abstract, meaning you cannot create instances directly from them. Instead, what you can do is declare variables with the interface type and then assign them to instances of types that happen to implement all the methods declared within the interface. 

Now, one of the really interesting things that Go does when working with interfaces, is that it allows types to implicitly implement an interface if that type implements the full range of method signatures within the interface. Let's see this in action. In the following example, a custom device interface is declared containing a single method named turnOn. I then declare two custom struct types, an iphone struct and an imac struct. 

Both structs contain the two fields name and model. Now, on lines 19 to 21 and lines 23 to 25, I implement two methods with different receiver arguments, one for the iphone struct and one for the imac struct, respectively. Both methods have the same turnOn name, which is also importantly the method name declared within the device interface, this being the turnOn method. The method implementations simply return a string indicating the flavor of the operating system that boots up when started. 

In this case, the receiver arguments are declared as non-pointer types since neither method is updating any of the data on the receiver argument within the body. Now, further down within the main function, I create instances of both the iphone and imac types on lines 28 and 29. Now, what follows is really the magic of working with interfaces, and demonstrates the deeper concept of polymorphism. Line 31 creates and initializes the device's slice, which importantly here is typed using the device interface. I then add both the iphone and imac struct instances to the device's slice. Finally, on lines 33, 34 and 35, I iterate over the items in the device's slice using the range keyword, and then for each item call its turnOn method. 

Again, keep in mind that both the iphone and imac items are actually different in terms of their types, but importantly they share the same common device interface since both implement the same method signature as declared within the device interface. This particular implicit typing system is an extremely useful tool and abstraction mechanism that aids the overall development experience when working with large code bases within Go. Let's now run this example and examine the resulting output. Here we can see the different boot up messages for each of the two different devices. 

In summary, you have just observed how to create custom interface types, and how having the capability to abstract behaviors into interfaces, as just demonstrated, helps to create reusable and maintainable code, particularly, again, in large code bases.

About the Author

Students31827
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