DEV Community

Cover image for Variables & Types
Samuel Grasse-Haroldsen
Samuel Grasse-Haroldsen

Posted on • Updated on

Variables & Types

Today we're going to talk about how to declare variables and some data types in the Go language.

Variables

Variables can be declared in a few different ways in Go. We can declare a variable with the var keyword, or if we want to declare a constant, we can use the keyword const.

var gopherAge = 11
const gopherMood = "happy"
Enter fullscreen mode Exit fullscreen mode

Declaring Type

We can also declare the type of our variable (identifier) if we want to. This is generally discouraged as the Go compiler can infer the type a variable should be by it's initial assignment. This is how we explicitly declare type.

var gopherAge int = 11
const gopherMood string = "happy"
Enter fullscreen mode Exit fullscreen mode

The Short Method

But there is another way we can declare variables. A more concise way. The "short method." NOTE: This shorter syntax is only available for non-constant variables.

gopherColor := "blue"
Enter fullscreen mode Exit fullscreen mode

Declaring Multiple Variables

We can also declare multiple variables using either of the following methods.

const (
        gopherCreator = "Renee French"
        gopherNumEyes = 2
)

// OR

const gopherCreator, gopherNumEyes = "Renee French", 2
Enter fullscreen mode Exit fullscreen mode

The Short Method Advantage

It is important to know that we can use the short method of variable declaration in places that the longer method (using var or const keywords) cannot be used.

for year := 2009; year < 2021; year++ {
        if age := year - 2009; age == 1 {
            fmt.Printf("In %v, the gopher was %v year old.\n", year, age)
        } else {
            fmt.Printf("In %v, the gopher was %v years old.\n", year, age)
        }
}

Enter fullscreen mode Exit fullscreen mode

We are declaring & initializing the variable year in our for loop and we are also declaring the variable age in our if statement. This is not possible using var or const.

Types

In a statically typed language such as Go, identifiers (variables) are associated with a specific type of data. There are a number of different types in Go, some basic common types include:

  1. int : a whole number (negative or positive)
  2. uint : a positive whole number
  3. float : real numbers (can contain decimals)
  4. bool : true or false
  5. string : a collection of characters

Gopher Bytes

It is hard to talk about data types without talking about how those variables are stored in memory. As we all know, computers just use 0s and 1s to do a whole range of things, so every time we declare a new variable, really we are just setting aside a certain amount of 0s and 1s (bits) for the computer to store something for us. This can be a name, an address, an age, anything. In Go, the compiler will take care of inferring what type of data we want, but we can explicitly declare what type of data a variable should contain. Go allows us to get even more granular and declare how many bits we need:

var red, green, blue uint8 = 3, 236, 252
Enter fullscreen mode Exit fullscreen mode

Here we are declaring three variables of type int8: red, green, and blue (AKA an RGB value). Why we specified the type as uint8 is because RGB values are always whole numbers between 0 and 255. We could have just let the compiler infer a type of int, but that means our tiny RGB values would take up a whopping 32 or 64 bits of memory, depending on our computer's underlying architecture, instead of just the 8 they would need. Obviously modern computers can handle quite a bit of this inefficient memory usage, but if we need to, we can optimize and use only the bits we need. Another bonus is that if we try to assign one of these values to a value above 255, the compiler will give an overflow error.

Conclusion

Statically typed languages have many advantages. At first declaring types may seem mundane and unnecessarily complicated, but having that ability to declare types coupled with a compiler that can quickly check our code for errors gives us the ability to catch issues before testing, optimize where we want, and improve readability.

Top comments (0)