Image for post
Image for post
Photo by Mitchell Luo on Unsplash

Go was created in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson at Google. Go is an open-source, statically typed, compiled language and is commonly referred to as Golang. Some of Go’s key benefits include “…memory safety, garbage collection, structural typing, and CSP-style concurrency” (Built In).

This blog is designed to show how to work with variables in Go and will cover the following topics:

  1. Declaring variables
  2. Redeclaring variables
  3. Shadowing
  4. Naming variables
  5. Scope
  6. Converting data types

**Some examples and overall structure of this blog come from Free Code Camp’s “Learn Go Programming — Golang Tutorial for Beginners” course on YouTube. **

If we wanted to declare a variable called i that has integer as its type, we could write the following.

package mainimport (     "fmt")func main() {     var i int}

Next, to assign this variable to an integer of 16 and print that variable to the console, we could write the below variable declaration. We’ll call declaring variables like this the multi-line approach.

package mainimport (     "fmt")func main() {     var i int
i = 16
fmt.Println(i)
}// Terminal command to run this program is go run main.goOutput: 16

Another way to write the above variable declaration would be to put everything on a single line. We’ll call declaring variables like this the single-line approach.

package mainimport (     "fmt")func main() {     var i int = 16     fmt.Println(i)}// Terminal command to run this program is go run main.goOutput: 16

Lastly, Go’s compiler is able to determine i’s data type based on the value we are setting i equal to with “:=”. In the example below, i will have a data type of integer. We’ll call declaring variables like this the colon-equals approach.

package mainimport ("fmt")func main() {     i := 16     fmt.Println(i)}// Terminal command to run this program is go run main.goOutput: 16

Now that we know that there are multiple ways to declare variables; multi-line, single-line, and colon-equals approaches. We should discuss when to use each type.

The multi-line approach is useful when you want to declare a variable but don’t want to initialize it. For example, the value of the variable is being assigned in a for loop later in the function, and making it equal to something outside the loop is unnecessary.

The single-line approach should be used when Go doesn’t have enough information to assign your desired data type. For example, say that we want our i variable from before to be a float32 instead of an integer. We could simply change our variable declaration to var i float32 = 16 . Declaring variables in this way provides us with more control. It is also worth noting that if we are declaring variables at the package level we need to use this type of syntax because the colon-equals approach is not allowed by Go for package level variables.

The colon-equals approach is the least verbose approach and should be used within our functions when Go’s compiler is able to determine the correct data type of the variable.

package mainimport (      "fmt")func main() {// multi-line approach: useful for for loops     var i int     i = 14// single-line: useful for when you need control over the data type     var j float32 = 15// colon-equals: least verbose, use when Go's compiler is able to determine data type     k := 16     fmt.Println(i)// print statements for displaying value and type     fmt.Printf("%v, %T", j, j)     fmt.Printf("%v, %T", k, k)}Output: 14
15, float32
16, int

At the package level, we can also assign multiple variables together in a block. Notice how in the block, we don’t need to type the var keyword in front of each variable name. We can also assign multiple blocks at the package level. Creating block variables is useful when we want to show that certain variables are related to each other. See below for an example.

package mainimport "fmt"var (     playerName    string  = "Andre Agassi"     sponsor       string  = "Head"     worldRanking  int     = 1     stringTension float32 = 65.5)var (     matchesPlayed int  = 0)func main() {     fmt.Println(playerName, sponsor, worldRanking, stringTension, 
matchesPlayed)
}Output:Andre Agassi Head 1 65.5 0

Take a look at the code below. What do you think the console will output?

  1. 27
  2. 42
  3. 13
  4. An Error
package mainimport "fmt"
var i int = 27
func main() { var i int = 42 i := 13 fmt.Println(i)}

If you guessed that this would produce an error, great job. The reason that this code creates an error is that we cannot declare the same variable twice in the same scope. In this instance, i := is trying to declare and initialize a new variable called i. However, we declared i already in our main function and set it equal to 42.

To get the console to output to 13 we simply would need to replace := with just = . This reassigns the value of i to 13 rather than making a new variable and setting that variable equal to 13.

In order for the above to output 42. We would need to remove the i := 13 or i = 13 , if we fixed the error. Even though we already declared i in the package level our second i declaration is in a different scope. Go will output the variable with the inner most scope, which in our case is 42. The technical term for this in Go is shadowing. Moreover, the package level i is still available but being hidden by the variable declaration in the main function.

To output 27 we would need to print i before we declare i equal to 42 in the main function. See below for how this would look.

package mainimport "fmt"var i int = 27func main() {     fmt.Println(i)

var i int = 42
fmt.Println(i)}Output: 27
42

**Special Note: Go requires that we use all the variables we declare in our application functions. If we don’t use a variable, the application will produce a compile-time error. This is useful as it forces developers to remove unused code and keep applications clean. **

Naming Convention Determines Visibility

The naming convention of our variables is important because it determines the variables visibility within the application. The three naming conventions for variables are:

  1. Lowercase
  2. Uppercase
  3. Block

Lowercase variable names are scoped to the package. This means that the variable is only accessible in the package in which it’s declared and unavailable for use in any other package.

Uppercase variables are globally visible, which means that they can be used within the package in which they are declared and other packages within our application. This is because uppercase variables are exported from the package when compiled.

Block scope can be found when we declare variables within functions. This scope is only visible within the function and not the package or other packages.

See below for an example of all naming conventions types

package mainimport "fmt"//uppercase: globally visiblevar I int = 27// lowercase: visible to the package onlyvar (     playerName    string  = "Andre Agassi"     sponsor       string  = "Head"     worldRanking  int     = 1     stringTension float32 = 65.5)func main() {// block: only visible within the main function     var i int = 42     fmt.Println(i)     fmt.Println(I)     fmt.Println(playerName, sponsor, worldRanking, stringTension)}Output:42
27
Andre Agassi Head 1 65.5

The last topic we will discuss in this blog is how to convert data types in Go. More specifically, we will cover converting integers to floats and converting integers to strings, which require different approaches.

Integers to Floats

To convert an integer to a float in Go you need to use a conversion operator. Go requires that you explicitly convert data types because it does not want to risk losing data through a conversion. In our example below, we will use float32 as a function to convert variable i to a float called j.

package mainimport "fmt"func main() {     var i int = 42     fmt.Printf("%v, %T\n", i, i)     var j float32     // conversion operator     j = float32(i)     fmt.Printf("%v, %T\n", j, j)}Output:42, int
42, float32

** Note: Converting from a float to an integer can result in data loss**

Integers to Strings

To convert an integer to a string in Go we need to add a package called “strconv”; found here. If we tried to use the conversion operator from before, we would get a unicode character. See bold items below for how to import the package and convert our integer to a string.

package mainimport ("fmt""strconv")func main() {     var i int = 42     fmt.Printf("%v, %T\n", i, i)     var j string     // use strconv package's str converter function to change from
an integer to a string
j = strconv.Itoa(i) fmt.Printf("%v, %T\n", j, j)}Output:42, int
42, string

I hope this blog has kickstarted your journey as a Go Developer. Be on the lookout for next week’s blog, where I will deep dive into Go’s data types. Until then, I have included some resources and learning materials for further exploration.

Go Playground: Alternative for downloading Go on your local machine. Great resource for quick practice.

Go Packages: List of packages in Go

“Learn Go Programming — Golang Tutorial for Beginners.” YouTube, freecodecamp.org, 20 June 2019, www.youtube.com/watch?v=YS4e4q9oBaU.

“Why Go? 8 Engineers Discuss Golang’s Advantages & How They Use It.” Built In, 15 Oct. 2019, builtin.com/software-engineering-perspectives/golang-advantages.

Written by

Software Engineer — Full Stack, JavaScript, ReactJS, Ruby on Rails, OO Programming (https://www.matthewsedlacek.com/)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store