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

Methods on Structs

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] Although Go isn't strictly an object oriented program language like Java and C# are, it still provides various language features that allow for an object oriented style of programming. 

One such feature is the concept of a method. A Go method is defined and associated with a receiver type allowing you to use dot notation to invoke the method on the receiver type. Let's see how this works. For starters, methods in Go are identical to functions syntaxwise with the one exception of an added receiver argument. The receiver in most cases will be declared as a struct type. Having said that, receivers can also be non-struct types as you'll see in the next part. The receiver argument is declared with a name and type, inserts inside parentheses appearing immediately after the func keyword and before the method name. 

A method receiver argument can be declared as a pointer or non-pointer type. There are two primary reasons to use a pointer receiver. The first is so that the method can modify the data that its receiver points to and the second is to avoid copying the value on each method core which as we know is a more efficient approach if the receiver is a large struct type. In the example provided here, all of them point to three methods: The fullname method, implemented on lines 11 to 13, the canDrive method, implemented on lines 15 to 21, and the updateAge method, implemented on lines 23, 24 and 25. 

All methods have as the receiver a variable named p which is a pointer to a person type. The person data type as declared earlier as a struct spanning on lines five through to nine. The fullname method simply concatenates the person's first name and surname and returns it as the person's full name. The canDrive method is used to test the person's age to see if they are 20 years or older and if so, then this method returns true. Otherwise it returns false. Methods are invoked using dot notation on the variable as long as the variable's datatype matches the method's receiver datatype. 

This can be seen on line 31 for person one and again on line 32, 34 and 37 for person two. This style of coding has the benefit of being easier to read and navigate. The receiver argument can be either a pointer to a type or just a plain type. With the pointer the a type option, having the advantage of allowing the method to modify the past and data such that the modifications last beyond the execution of the method. Knowing that the fullname in candDrive methods perform read-only operations on the underlined person struct, they then could in fact be declared to use non pointer receiver types like so. Running this example, produces the following output. 

In summary, you've observed methods are identical to functions, except for the added receiver argument inserted between the func keyword and the method's name. Receiver arguments are often custom struct types. Receiver arguments can be either value or reference types and dot notation is the preferred way to invoke a method.

About the Author

Students18965
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