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

Pointers - How

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

- Pointers and Go provide you with the ability to store the memory address of another variable. Why would you want to do this you may be asking yourself. I'll answer this in the next pointer based demo. But, for now let me continue explaining what they are and how you code with them. For starters, any variable you declare in a Go program requires memory to store the data or value assigned to the variable. The memory requirements in terms of size and space are entirely dependent on the type of the variable. Memory within a computer system is addressable and variables are used to meet themselves to the addressable location where the assigned data resides. 

A pointer is also a variable, but a special kind of one. That is because it stores a memory address rather than the data itself. The memory address is often the address of another variable. Let's quickly run this example and then next explain its inner workings. In the example provided here, I declare num one and num two as standard integer typed variables on lines six and seven and I assigned the values 100 and 200 respectively. Line eight declares the string one variable, which is initialized with the string value "blah." A pointer to an integer is declared on line 10 and is initialized with the memory address of the num one variable.

Pointers are declared by prefixing an asterisks to the type that the pointer is pointing to. An ampersand is prefixed to a variable to get its memory address. It is important to realize that in this case, pointer one can only store the memory address of int variables. If you were to try and assign the memory address of a different data type, then a compiled time error would be thrown. Lines 13, 14 and 15 print out the memory addresses for the num one, num two, and string one variables, respectively. Line 17 then prints out the memory address stored in pointer one. Notice that it stores the same num one memory address. We can then de=reference pointer one by prefixing it with an asterisk, as is done on line 19, and change its stored value. In this case, we are changing the value from 100 to 101. 

Now the cool thing about doing this is that any other variable that points to the same memory location will see this update. Line 20 prints out the num one variable and as expected, we can see that it prints out 101. Pointers can be updated to point at different memory addresses, even after initialization, as shown on line 22. Here, pointer one now points at the memory address of the num two variable. When we de-reference pointer one again, this time it prints out 200, which is the value held in the memory location of variable num two. Pointers can also be created using the in built new function, as shown on line 26. Here, pointer two is created and initialized using shorthand notation, and is declared to also be a pointer to an int. Line 27 simply tells pointer two to point to the same memory address that pointer one currently points to. 

Finally, if we were to de-reference pointer two, it will again print out 200, which is the value held in the memory location of variable num two. 

In summary, you have just observed that pointers are variables that store memory addresses of other variables. Pointers are typed. A memory address for a given variable is found by prefixing the variable's name with an ampersand, and how to create a new pointer using the built in new function. The new function speaks a type as an input parameter, and then allocates just enough memory to values of that type. A couple of notes about pointers. The zero value of a pointer is null. This means any uninitialized pointer will have the value null, and there is no pointer arithmetic and go.

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