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

String Variables

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] In this demo, I'll introduce you to the concept of variables and how they are declared and used. 

For starters, I'll focus on declaring variables of type string. In the following demonstrations, I'll cover off the remaining primitive data types such as numbers and booleans, etc. The Go language allows variables to be created multiple ways. In general, a variable is declared by using the var keyword followed by the name of the variable and then its type. Go, however, provides a few minor variations to this syntax which I'll explain and demonstrate. 

The first approach used with this code sample involves using the var keyword with parentheses to group multiple variables together. This can be seen on lines seven through to 10 where the var keyword groups and declares the variables Name1 and Name2, both typed as strings. Name1 and Name2 are considered global variables not just in the current file but anywhere within the wider application since they have been declared outside of the main function and their names begin with a capital letter. More on this later. Line 12 shows the next approach whereby several variables sharing the same type are declared together on the same line. 

This approach only works when all variables are of the same type. Var1 and var2 have global scope only within the current file as both their names start with a lower case character. Moving into the main function which starts on line 14, we can see an occurrence of a string variable that's been declared but not assigned an initial value. When this happens the compiler will assign the variable with what's referred to as its zero value, which for a string is an empty string. Since this variable was declared within the main function, its scope is limited to the main function. When the main function completes executing, this variable and all other variables declared within the function will cease to exist. On line 20 this variable is printed out, and since its still not been explicitly assigned a string, it will print out the empty string zero value. Line 16 declares a second string variable named name2, which is initialized immediately with the string value "CloudAcademy". 

Lines 17 and 18 show a shortened form of creating and initializing a variable. In this case, each variable has its data type inferred by the compiler at compile time. The go compiler infers the data type by examining the datatype of the value that they have been initialized with. In this case, both name3 and name4 are inferred as being variables of the string data type. Now let's run this example. As we can see in the output, line 20 resulted in printing an empty string since the variable name1 was never explicitly initialized, and is currently set to its zero value, in this case the empty string. On line 21, we reassign the name1 variable to now hold the value "Blah" and then this is printed out by the line 22 print line statement. Lines 24, 25, and 26 print out the respective string values held by variables name2, name3, and name4, with the name4 variable being an example of string concatenation of variables name2, a space, and name3. 

Line 28 demonstrates how to use the string formatters Printf function to perform string interpolation using string variables name2 and name3. Finally, on line 30 the global variables declared outside of the main function are printed out. 

Therefore, in summary I've shown you the basic syntax form for declaring a variable using the var keyword, how to use the var keyword to declare a variable of type string, how to initialize a variable of type string, how a string variable is initialized to its zero value, in this case an empty string when you don't explicitly initialize it yourself, how to declare and initialize a string variable using the shorthand syntax, how to use the var keyword to group variable declaration together, and how to use the Printf function on the string formatter library to perform string interpolation. 

A few notes. There are a few rules when it comes to naming variables. Names must consist of only alphanumeric characters only, with the added exception of the underscore character. All other characters are prohibited. Names cannot start with a number. And visibility of variables is controlled via a naming convention which utilizes the first name of the variable name. If it is a capital letter and the variable was declared outside of the main function, then it is exported and visible elsewhere.

About the Author

Students18960
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