i Go – Variables – All things in moderation

Go – Variables

Variable

A variable is a storage area that the programs can manipulate, a variable include of two parts: variable name and data type.A variable is a storage area that the programs can manipulate, a variable include of two parts: variable name and data type.

For example:

package main

import "fmt"

func main() {
var x string = "Hello World"
fmt.Println(x)
}

In above code, rather than send message directly to the Println function we assign it to a variable instead. Variables in Go are created by first using the var keyword, then specifying the variable name (x), the type (string) and finally assigning a value to the variable (Hello World).

In programming, we declare a variable and then assign it to a value, so Go allows us to declare and assign a quick value as follows:

x := "Hello World"

Here we do not use the var keyword, do not declare the data type, instead we write the variable name and then use the operator: = followed by the value to quickly declare a variable and assign the value on the spot. The Go compiler automatically recognizes the data type based on the value you assign to the variable. For example, here Go see the value as “Hello World”.

2 lines following  are the same :

var x string = "Hello World"
x := "Hello World"

Name a Variable

Variable names may have one or more characters, may contain letters, underscores _ and numbers. The first character must be a letter or an underscore.

Golang doesn’t care what you name a variable so the name is meant for your (and others) benefit. Pick names which clearly describe the variable’s purpose. For example:

title := "Hydrasky"

fmt.Println("Website name: ", title)

Scope of Variables

Going back to the program we saw at the beginning of the chapter:

package main

import "fmt"

func main() {
  var x string = "Hello World"
  fmt.Println(x)
}

Another way of writing this program would be like this:

package main

import "fmt"

var x string = "Hello World"

func main() {
  fmt.Println(x)
}

Notice that we moved the variable outside of the main function. This means that other functions can access this variable:

var x string = "Hello World"

func main() {
  fmt.Println(x)
}

func f() {
  fmt.Println(x)
}

The f function now has access to the x variable. Now suppose that we wrote this instead:

func main() {
  var x string = "Hello World"
  fmt.Println(x)
}

func f() {
  fmt.Println(x)
}

If you run this program you should see an error:

.\main.go:11: undefined: x

The compiler is telling you that the x variable inside of the f function doesn’t exist. It only exists inside of the main function. The range of places where you are allowed to use x is called the scope of the variable. According to the language specification “Go is lexically scoped using blocks”. Basically this means that the variable exists within the nearest curly braces { } (a block) including any nested curly braces (blocks), but not outside of them. Scope can be a little confusing at first; as we see more Go examples it should become more clear.

Constants Variables

Go also has support for constants. Constants are basically variables whose values cannot be changed later. They are created in the same way you create variables but instead of using the var keyword we use the const keyword:

package main

import "fmt"

func main() {
  const x string = "Hello World"
  fmt.Println(x)
}

This:

const x string = "Hello World"
x = "Some other string"

Results in a compile-time error:

.\main.go:7: cannot assign to x

Constants are a good way to reuse common values in a program without writing them out each time. For example Pi in the math package is defined as a constant.

Multiple Variables

Go also has another shorthand when you need to define multiple variables:

var (
  a = 5
  b = 10
  c = 15
)

Use the keyword var (or const) followed by parentheses with each variable on its own line.

Leave a Reply