image
Scopes and Shadowing
Start course
Difficulty
Beginner
Duration
2h 17m
Students
3927
Ratings
4.6/5
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] The following Go code emphasizes the concept of scope and variable shadowing. 

Lets first run this code sample so that you can correlate the explanation with the programs output. Here we can see the following output has been produced, within the main function there are six cores to the print function scattered throughout the implementation to print out the current value of the variable X, which undergoes shadowing due to the fact that the variable X is declared and initialized multiple times within different scopes. Within the util dot Go file variable X is first declared and initialized with the value eleven on line four. Note here that X is being declared in package scope within the main package which happens to span both the util dot Go, and the main dot Go files. 

Jumping down into the main function declared within the main dot Go file. We can see that X is re declared on line eighteen. This is permitted since we are doing it within a new scope, that being the main function scope. Lines twenty two to twenty nine define an anonymous function which takes a named input perimeter X, the anonymous function is called with the value five on line twenty nine. Therefore X takes on the value five within the anonymous function as it starts. Within this anonymous function an F statement is declared on line twenty four, which happens to declare X again it initializes it with the value three. 

This is an implicate blocked scoped declaration, and in this case the execution will enter into, the F statement since X is indeed three. Exiting back out of the F statement X retains the value five since the implicitly scoped X associated with the F statement is now deceases being no longer in scope. Finally as we exit out of the anonymous function X retains the value two, since the enormous function scope has been completed, and we're back within the main function scope. While we have this example loaded lets edit, end on comment line twenty five, within the F statement. This is an example of an explicitly scoped declaration of X within the F statement. Rewriting this example now changes the value printed out in the fourth print statement within the programs output. 

In summary you have observed how variables can be shadowed, and what happens when they are. How scope sits and controls the lifetime of a variable, how a same named variable can exist in different scopes. How packaged scopes can't be in multiple files, and how different scopes exist within a program, and can be nested.

About the Author
Students
143181
Labs
69
Courses
109
Learning Paths
209

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