How To Use Variables and Constants in Go

Written by Gopher Guides

Variables are an important programming concept to master. They are symbols that stand in for a value you’re using in a program.

This tutorial will cover some variable basics and best practices for using them within the Go programs you create.

Understanding Variables

In technical terms, a variable is assigning a storage location to a value that is tied to a symbolic name or identifier. We use the variable name to reference that stored value within a computer program.

We can think of a variable as a label that has a name on it, which you tie onto a value.

14. How To Use Variables and Constants in Go - 图1

Variables in Go

Let’s say we have an integer, 1032049348, and we want to store it in a variable rather than continuously retype the long number over and over again. To achieve this, we can use a name that’s easy to remember, like the variable i. To store a value in a variable, we use the following syntax:

  1. i := 1032049348

We can think of this variable like a label that is tied to the value.

14. How To Use Variables and Constants in Go - 图2

Go Variable Example

The label has the variable name i written on it, and is tied to the integer value 1032049348.

The phrase i := 1032049348 is a declaration and assignment statement that consists of a few parts:

  • the variable name (i)
  • the short variable declaration assignment (:=)
  • the value that is being tied to the variable name (1032049348)
  • the data type inferred by Go (int)

We’ll see later how to explicitly set the type in the next section.

Together, these parts make up the statement that sets the variable i equal to the value of the integer 1032049348.

As soon as we set a variable equal to a value, we initialize or create that variable. Once we have done that, we are ready to use the variable instead of the value.

Once we’ve set i equal to the value of 1032049348, we can use i in the place of the integer, so let’s print it out:

  1. package main
  2. import "fmt"
  3. func main() {
  4. i := 1032049348
  5. fmt.Println(i)
  6. }

Output

  1. 1032049348

We can also quickly and easily do math by using variables. With i := 1032049348, we can subtract the integer value 813 with the following syntax:

  1. fmt.Println(i - 813)

Output

  1. 1032048535

In this example, Go does the math for us, subtracting 813 from the variable i to return the sum 1032048535.

Speaking of math, variables can be set equal to the result of a math equation. You can also add two numbers together and store the value of the sum into the variable x:

  1. x := 76 + 145

You may have noticed that this example looks similar to algebra. In the same way that we use letters and other symbols to represent numbers and quantities within formulas and equations, variables are symbolic names that represent the value of a data type. For correct Go syntax, you’ll need to make sure that your variable is on the left side of any equations.

Let’s go ahead and print x:

  1. package main
  2. import "fmt"
  3. func main() {
  4. x := 76 + 145
  5. fmt.Println(x)
  6. }

Output

  1. 221

Go returned the value 221 because the variable x was set equal to the sum of 76 and 145.

Variables can represent any data type, not just integers:

  1. s := "Hello, World!"
  2. f := 45.06
  3. b := 5 > 9 // A Boolean value will return either true or false
  4. array := [4]string{"item_1", "item_2", "item_3", "item_4"}
  5. slice := []string{"one", "two", "three"}
  6. m := map[string]string{"letter": "g", "number": "seven", "symbol": "&"}

If you print any of these variables, Go will return what that variable is equivalent to. Let’s work with the assignment statement for the string slice data type:

  1. package main
  2. import "fmt"
  3. func main() {
  4. slice := []string{"one", "two", "three"}
  5. fmt.Println(slice)
  6. }

Output

  1. [one two three]

We assigned the slice value of []string{"one", "two", "three"} to the variable slice, and then used the fmt.Println function to print out that value by calling slice.

Variables work by carving out a little area of memory within your computer that accepts specified values that are then associated with that space.

Declaring Variables

In Go, there are several ways to declare a variable, and in some cases, more than one way to declare the exact same variable and value.

We can declare a variable called i of data type int without initialization. This means we will declare a space to put a value, but not give it an initial value:

  1. var i int

This creates a variable declared as i of data type int.

We can initialize the value by using the equal (=) operator, like in the following example:

  1. var i int = 1

In Go, both of these forms of declaration are called long variable declarations.

We can also use short variable declaration:

  1. i := 1

In this case, we have a variable called i, and a data type of int. When we don’t specify a data type, Go will infer the data type.

With the three ways to declare variables, the Go community has adopted the following idioms:

  • Only use long form, var i int, when you’re not initializing the variable.

  • Use short form, i := 1, when declaring and initializing.

  • If you did not desire Go to infer your data type, but you still want to use short variable declaration, you can wrap your value in your desired type, with the following syntax:

  1. i := int64(1)

It’s not considered idiomatic in Go to use the long variable declaration form when we’re initializing the value:

  1. var i int = 1

It’s good practice to follow how the Go community typically declares variables so that others can seamlessly read your programs.

Zero Values

All built-in types have a zero value. Any allocated variable is usable even if it never has a value assigned. We can see the zero values for the following types:

  1. package main
  2. import "fmt"
  3. func main() {
  4. var a int
  5. var b string
  6. var c float64
  7. var d bool
  8. fmt.Printf("var a %T = %+v\n", a, a)
  9. fmt.Printf("var b %T = %q\n", b, b)
  10. fmt.Printf("var c %T = %+v\n", c, c)
  11. fmt.Printf("var d %T = %+v\n\n", d, d)
  12. }

Output

  1. var a int = 0
  2. var b string = ""
  3. var c float64 = 0
  4. var d bool = false

We used the %T verb in the fmt.Printf statement. This tells the function to print the data type for the variable.

In Go, because all values have a zero value, we can’t have undefined values like some other languages. For instance, a boolean in some languages could be undefined, true, or false, which allows for three states to the variable. In Go, we can’t have more than two states for a boolean value.

Naming Variables: Rules and Style

The naming of variables is quite flexible, but there are some rules to keep in mind:

  • Variable names must only be one word (as in no spaces).
  • Variable names must be made up of only letters, numbers, and underscores (_).
  • Variable names cannot begin with a number.

Following these rules, let’s look at both valid and invalid variable names:

ValidInvalidWhy Invalid
userNameuser-nameHyphens are not permitted
name44nameCannot begin with a number
user$userCannot use symbols
userNameuser nameCannot be more than one word

Furthermore, keep in mind when naming variables that they are case sensitive. These names userName, USERNAME, UserName, and uSERnAME are all completely different variables. It’s best practice to avoid using similar variable names within a program to ensure that both you and your collaborators—current and future—can keep your variables straight.

While variables are case sensitive, the case of the first letter of a variable has special meaning in Go. If a variable starts with an uppercase letter, then that variable is accessible outside the package it was declared in (or exported). If a variable starts with a lowercase letter, then it is only available within the package it is declared in.

  1. var Email string
  2. var password string

Email starts with an uppercase letter and can be accessed by other packages. password starts with a lowercase letter, and is only accessible inside the package it is declared in.

It is common in Go to use very terse (or short) variable names. Given the choice between using userName and user for a variable, it would be idiomatic to choose user.

Scope also plays a role in the terseness of the variable name. The rule is that the smaller the scope the variable exists in, the smaller the variable name:

  1. names := []string{"Mary", "John", "Bob", "Anna"}
  2. for i, n := range names {
  3. fmt.Printf("index: %d = %q\n", i, n)
  4. }

We use the variable names in a larger scope, so it would be common to give it a more meaningful name to help remember what it means in the program. However, we use the i and n variables immediately in the next line of code, and then do not use them again.. Because of this, it won’t confuse someone reading the code about where the variables are used, or what they mean.

Next, let’s cover some notes about variable style. The style is to use MixedCaps or mixedCaps rather than underscores for multi-word names.

Conventional StyleUnconventional StyleWhy Unconventional
userNameuser_nameUnderscores are not conventional
iindexprefer i over index as it is shorter
serveHTTPserveHttpacronyms should be capitalized

The most important thing about style is to be consistent, and that the team you work on agrees to the style.

Reassigning Variables

As the word “variable” implies, we can change Go variables readily. This means that we can connect a different value with a previously assigned variable through reassignment. Being able to reassign is useful because throughout the course of a program we may need to accept user-generated values into already initialized variables. We may also need to change the assignment to something previously defined.

Knowing that we can readily reassign a variable can be useful when working on a large program that someone else wrote, and it isn’t clear what variables are already defined.

Let’s assign the value of 76 to a variable called i of type int, then assign it a new value of 42:

  1. package main
  2. import "fmt"
  3. func main() {
  4. i := 76
  5. fmt.Println(i)
  6. i = 42
  7. fmt.Println(i)
  8. }

Output

  1. 76
  2. 42

This example shows that we can first assign the variable i with the value of an integer, and then reassign the variable i assigning it this time with the value of 42.

Note: When you declare and initialize a variable, you can use :=, however, when you want to simply change the value of an already declared variable, you only need to use the equal operator (=).

Because Go is a typed language, we can’t assign one type to another. For instance, we can’t assign the value "Sammy" to a variable of type int:

  1. i := 72
  2. i = "Sammy"

Trying to assign different types to each other will result in a compile-time error:

Output

  1. cannot use "Sammy" (type string) as type int in assignment

Go will not allow us to use a variable name more than once:

  1. var s string
  2. var s string

Output

  1. s redeclared in this block

If we try to use short variable declaration more than once for the same variable name we’ll also receive a compilation error. This can happen by mistake, so understanding what the error message means is helpful:

  1. i := 5
  2. i := 10

Output

  1. no new variables on left side of :=

Similarly to variable declaration, giving consideration to the naming of your variables will improve the readability of your program for you, and others, when you revisit it in the future.

Multiple Assignment

Go also allows us to assign several values to several variables within the same line. Each of these values can be of a different data type:

  1. j, k, l := "shark", 2.05, 15
  2. fmt.Println(j)
  3. fmt.Println(k)
  4. fmt.Println(l)

Output

  1. shark
  2. 2.05
  3. 15

In this example, the variable j was assigned to the string "shark", the variable k was assigned to the float 2.05, and the variable l was assigned to the integer 15.

This approach to assigning multiple variables to multiple values in one line can keep the number of lines in your code down. However, it’s important to not compromise readability for fewer lines of code.

Global and Local Variables

When using variables within a program, it is important to keep variable scope in mind. A variable’s scope refers to the particular places it is accessible from within the code of a given program. This is to say that not all variables are accessible from all parts of a given program—some variables will be global and some will be local.

Global variables exist outside of functions. Local variables exist within functions.

Let’s take a look at global and local variables in action:

  1. package main
  2. import "fmt"
  3. var g = "global"
  4. func printLocal() {
  5. l := "local"
  6. fmt.Println(l)
  7. }
  8. func main() {
  9. printLocal()
  10. fmt.Println(g)
  11. }

Output

  1. local
  2. global

Here we use var g = "global" to create a global variable outside of the function. Then we define the function printLocal(). Inside of the function a local variable called l is assigned and then printed out. The program ends by calling printLocal() and then printing the global variable g.

Because g is a global variable, we can refer to it in printLocal(). Let’s modify the previous program to do that:

  1. package main
  2. import "fmt"
  3. var g = "global"
  4. func printLocal() {
  5. l := "local"
  6. fmt.Println(l)
  7. fmt.Println(g)
  8. }
  9. func main() {
  10. printLocal()
  11. fmt.Println(g)
  12. }

Output

  1. local
  2. global
  3. global

We start by declaring a global variable g, var g = "global". In the main function, we call the function printLocal, which declares a local variable l and prints it out, fmt.Println(l). Then, printLocal prints out the global variable g, fmt.Println(g). Even though g wasn’t defined within printLocal, it could still be accessed because it was declared in a global scope. Finally, the main function prints out g as well.

Now let’s try to call the local variable outside of the function:

  1. package main
  2. import "fmt"
  3. var g = "global"
  4. func printLocal() {
  5. l := "local"
  6. fmt.Println(l)
  7. }
  8. func main() {
  9. fmt.Println(l)
  10. }

Output

  1. undefined: l

We can’t use a local variable outside of the function it is assigned in. If you try to do so, you’ll receive a undefined error when you compile.

Let’s look at another example where we use the same variable name for a global variable and a local variable:

  1. package main
  2. import "fmt"
  3. var num1 = 5
  4. func printNumbers() {
  5. num1 := 10
  6. num2 := 7
  7. fmt.Println(num1)
  8. fmt.Println(num2)
  9. }
  10. func main() {
  11. printNumbers()
  12. fmt.Println(num1)
  13. }

Output

  1. 10
  2. 7
  3. 5

In this program, we declared the num1 variable twice. First, we declared num1 at the global scope, var num1 = 5, and again within the local scope of the printNumbers function, num1 := 10. When we print num1 from the main program, we see the value of 5 printed out. This is because main only sees the global variable declaration. However, when we print out num1 from the printNumbers function, it sees the local declaration, and will print out the value of 10. Even though printNumbers creates a new variable called num1 and assigned it a value of 10, it does not affect the global instance of num1 with the value of 5.

When working with variables, you also need to consider what parts of your program will need access to each variables; adopting a global or local variable accordingly. Across Go programs, you’ll find that local variables are typically more common.

Constants

Constants are like variables, except they can’t be modified once they have been declared. Constants are useful for defining a value that will be used more than once in your program, but shouldn’t be able to change.

For instance, if we wanted to declare the tax rate for a shopping cart system, we could use a constant and then calculate tax in different areas of our program. At some point in the future, if the tax rate changes, we only have to change that value in one spot in our program. If we used a variable, it is possible that we might accidentally change the value somewhere in our program, which would result in an improper calculation.

To declare a constant, we can use the following syntax:

  1. const shark = "Sammy"
  2. fmt.Println(shark)

Output

  1. Sammy

If we try to modify a constant after it was declared, we’ll get a compile-time error:

Output

  1. cannot assign to shark

Constants can be untyped. This can be useful when working with numbers such as integer-type data. If the constant is untyped, it is explicitly converted, where typed constants are not. Let’s see how we can use constants:

  1. package main
  2. import "fmt"
  3. const (
  4. year = 365
  5. leapYear = int32(366)
  6. )
  7. func main() {
  8. hours := 24
  9. minutes := int32(60)
  10. fmt.Println(hours * year)
  11. fmt.Println(minutes * year)
  12. fmt.Println(minutes * leapYear)
  13. }

Output

  1. 8760
  2. 21900
  3. 21960

If you declare a constant with a type, it will be that exact type. Here when we declare the constant leapYear, we define it as data type int32. Therefore it is a typed constant, which means it can only operate with int32 data types. The year constant we declare with no type, so it is considered untyped. Because of this, you can use it with any integer data type.

When hours was defined, it inferred that it was of type int because we did not explicitly give it a type, hours := 24. When we declared minutes, we explicitly declared it as an int32, minutes := int32(60).

Now let’s walk through each calculation and why it works:

  1. hours * year

In this case, hours is an int, and years is untyped. When the program compiles, it explicitly converts years to an int, which allows the multiplication operation to succeed.

  1. minutes * year

In this case, minutes is an int32, and year is untyped. When the program compiles, it explicitly converts years to an int32, which allows the multiplication operation to succeed.

  1. minutes * leapYear

In this case, minutes is an int32, and leapYear is a typed constant of int32. There is nothing for the compiler to do this time as both variables are already of the same type.

If we try to multiply two types that are typed and not compatible, the program will not compile:

  1. fmt.Println(hours * leapYear)

Output

  1. invalid operation: hours * leapYear (mismatched types int and int32)

In this case, hours was inferred as an int, and leapYear was explicitly declared as an int32. Because Go is a typed language, an int and an int32 are not compatible for mathematical operations. To multiply them, you would need to convert one to a int32 or an int.

Conclusion

In this tutorial we reviewed some of the common use cases of variables within Go. Variables are an important building block of programming, serving as symbols that stand in for the value of a data type we use in a program.