Understanding the Basics

In programming, variables are essential for storing and manipulating data in your programs. Go is no exception, and it provides a robust and flexible mechanism for declaring and using variables. In this article, we’ll take a closer look at variables in Go and understand the basic syntax for declaring and using them.

Declaring Variables

In Go, variables are declared using the var keyword, followed by the variable name and its type. If you read the Basic Syntax article I posted previously this should look familiar to you.

var age int

This declares a variable age of type int. Variables can also be initialized with a value when they’re declared:

var age int = 30

This declares a variable age of type int and initializes it with the value 30

Shorthand Declaration

Go also provides a shorthand for declaring variables, which is more concise and easier to read. The shorthand syntax uses the := operator to declare and initialize variables in a single line. For example:

age := 30

As you can see we don’t use the var keyword here but instead we make use of the := operator to both declare and initialize the variable. There is however a caveat to using this approach. This can only be used inside of a function block. For example:

package main

import "fmt"

func main() {
    age := 34
    name := "Bowser"
    fmt.Println("My name is", name, "I am", age, "years old")
}

This will work. I have declared both the age variable which is set to 34, an int type, as well as the name variable which is of type string. I then use the fmt.Println function to print a message to the terminal. One other thing you can see from this example is that we don’t have to use the concatenation operator, + , that is used in other languages to combine variables and strings together to form a message. Just separate out the values using a , and you are good to go.

An incorrect usage of the shorthand declaration would be the following:

pakcage main

import "fmt"

age := 34
name := "Bowser"

func main() {
    fmt.Println("My name is", name, "I am", age, "years old.")
}

The above code would break. If you aren’t declaring variables within a block scope, or a function scope, then you need to use the var keyword. So the above correctly written, would be:

package main

import "fmt"

var age int = 34
var name string = "Bowser"

func main() {
    fmt.Println("My name is", name, "I am", age, "years old.}

Type Inference

Go’s type inference mechanism means that the type of a variale can be inferred from the value assigned to it. For example:

age := 30

In this example Go automatically infers the type of age as int.

Scop of Variables

The scope of a variable in Go refers to the part of the program where the variable can be accessed. Variables declared within a function have local scope, and they’re only accessible within the function. Variables declared outside of any function have global scope and can be accessed from anywhere in the program.

Conclusion

Variables are an essential part of any programming language, and Go provides a flexible and easy-to-use mechanism for declaring and using them. With its simple syntax, Go makes it easy to declare variables, and its type inference means that you don’t have to explicitly specify the type of a variable in many cases. Whether you’re a beginner or an experienced programmer, understanding variables in Go is an important step in becoming proficient with this language.