Hello!

Today we will talk about variables in golang. There are three ways to create variables, let’s try each of them.

Var keyword

The first way is to create a variable using the var keyword. This is how you can create a variable that will have no value (will have an initial value depending on variable type). It looks like this var PARAM_NAME PARAM_TYPE for example var level int

package main

import "fmt"


func main() {
    var level int
    fmt.Println(level)
}
$ go run main.go
0

Even if we did not specify a value for the level variable, it will still have a value. Now after a variable is created, it can be changed.

package main

import "fmt"

func main() {
    var level int
    level = 10
    fmt.Println(level)
}
$ go run main.go
10

Also, this variable can be moved outside of the function and the code will still work.

package main

import "fmt"

var level int

func main() {
    level = 10
    fmt.Println(level)
}

If you need to create several variables at the same time, you can do it this way in order not to repeat the word var

var (
    level int
    exp   int
    life  int
)

Var keyword with a value

Also, when creating a variable, an initial value can be set for it. var PARAM_NAME PARAM_TYPE = PARAM_VALUE, for example, var level int = 10.

package main

import "fmt"

func main() {
    var level int = 10
    fmt.Println(level)
}
$ go run main.go
10

Since we specify the value of the variable immediately, the type can be omitted as var level = 10 and the program will work without changes.

package main

import "fmt"

func main() {
    var level = 10
    fmt.Println(level)
}
$ go run main.go
10

This variable, like the previous one, can be moved outside the function and the code will work. If you need to create several variables at the same time, you can do it similarly as before

var (
    level = 10
    exp   = 300
    life  = 4
)

Variables without var

The third way for creating a variable is level := 10. The word var is no longer used here, but such variables can only be created inside functions

package main

import "fmt"

func main() {
    level := 10
    fmt.Println(level)
}
$ go run main.go
10

And if I try to take the variable outside the function, I get the following

go run main.go
# command-line-arguments
./main.go:5:1: syntax error: non-declaration statement outside the function body

Variables Naming

Usually in golang variables are named as follows. If the variable does not start with a capital letter, it means that it is available only within this package.

var level = 1

If the variable starts with a capital letter, then this variable becomes available for use in other packages

var Level = 1

If the variable consists of more than one word, then its name also depends on whether it should be available only inside the package or from others. The second word in the variable name must always begin with an uppercase letter

var heroLevel = 1
var HeroLevel = 1

Variables conversion

If we have a variable of one type, it can be converted to another type. For example, we have an int and we need it to be a float. This can be done as follows

package main

import "fmt"

func main() {
    var level int = 10
    levelFloat := float64(level)
    fmt.Printf("%v: %T", levelFloat, levelFloat)
}
go run main.go
10: float64

Now let’s try to do the same but with the string type

package main

import "fmt"

func main() {
    var level int = 123
    levelFloat := string(level)
    fmt.Printf("%v: %T", levelFloat, levelFloat)
}
go run main.go
{: string

The result is not exactly what we expected. When converting, golang takes the Unicode value under this number. And under the number 123 there is the value {.

So how to convert int to string? For this, go has special methods in the strconv package, for example strconv.Itoa to convert int to string

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var level int = 123
    levelFloat := strconv.Itoa(level)
    fmt.Printf("%v: %T", levelFloat, levelFloat)
}
$ go run main.go
123: string

Shadowing

If we have a global variable outside the function and we create a variable with the same name in the function, this is called shadowing. Let’s check an example

package main

import (
    "fmt"
)

var level int = 1

func main() {
    fmt.Println(level)
    var level = 20
    fmt.Println(level)
    printLevel()
}

func printLevel() {
    fmt.Println(level)
}

Here we have a package-level variable with a value of 1, we print this variable and then create another variable with the same name inside the function. And we print its value. We also have another method printLevel method that also prints the value of the level variable.

$ go run main.go
1
20
1

As can be seen from the result, first the output of the package level variable took place, then the output of the local variable from the function, and at the end the package variable again

Video