Introducing Variables in Golang

Golang comes with a decent offering around variables that you can use to store, retrieve and manipulate information.

A variable is defined with the var keyword followed by the name of the variable, the type and then the assignment itself.

How to name a variable in Golang

Variable names must:

  • Start with a letter
  • May contain letters, numbers or the underscore _ character.

Variable names that start with numbers or special characters are not allowed.

Number variable types

Let’s take a simple number, or integer as we often call it:

var aNumber int = 13
Code language: Go (go)

This declared a number, which we aptly call aNumber, declare it as a type Integer and then assign the value 13 to it.

Golang provides us with the ability to not have to type-cast variables all the time, by using the := assignment operator.

So taking the above example, let’s change it to use this new operator:

aNumber := 13
Code language: Go (go)

That was easy! What about other variable types you may ask… well, you can do it for everything!

Golang will also infer the type of initialised variables if we don’t specify them:

var aNumber = 13
Code language: Go (go)

Numbers also come with additional sizes.

For Integer sizes, we have:

int, int8, int16, int32 and int64

For Unsigned Integer sizes, we have:

uint, uint8, uint16, uint32, uint64 and uintptr

Additional number variable types

Additionally to our standard Integer types, there also exists:

byte which is simply an alias for uint8

rune which is an alias for int32 and represents a Unicode code point

Then we have float32 and float64 to store floating-point or decimal numbers.

As well as complex64 and complex128 for working with complex numbers as a whole.

String variable types

The string type creates a standard variable string.

var ourStringName string = "Introducing Variables in Golang"
Code language: Go (go)

or

var ourStringName = "Introducing Variables in Golang"
Code language: Go (go)

or

ourStringName := "Introducing Variables in Golang"
Code language: Go (go)

Boolean variable types

Booleans in Golang are either true or false.

We can set a Boolean to true by declaring similarly to how we did for a string earlier.

var ourBooleanName bool = true
Code language: Go (go)

or

var ourBooleanName = true
Code language: Go (go)

or

ourBooleanName := true
Code language: Go (go)

Scope of a variable

As with all other languages, variables have a scope.

This means that a variable can be accessed within the same scope that it is created in.

If you need to use a variable outside that creation scope, you will either need to pass it along when calling a function or method, or reference it using pointers.

package main import ( "fmt" ) func main() { // create a variable secret := 19872 // print this out => 19872 fmt.Println(secret) // call Function1, pass in secret anotherFunction1(secret) // call Function2, pass in reference to secret anotherFunction2(&secret) } // create a new variable to store secret func anotherFunction1(s int) { fmt.Println(s) } // use the same variable of the previous scope func anotherFunction2(s *int) { fmt.Println(*s) }
Code language: Go (go)

Pointers tends to make things quite a bit more complicated if you are not ready for the concept yet.

Pointers allow for variables to not have to be redeclared and therefore allows for smaller application execution space.

We will cover this in greater detail in a future tutorial.

Constants

Constants are simply variables that cannot, or should not, be changed after they have been initialised.

const ourStringName string = "Introducing Variables in Golang"
Code language: Go (go)

In software engineering, this is called an immutable type, as it is not able to change.

Multiple declarations

It is possible to declare multiple variables at once:

var a, b, c int = 1, 2, 3
Code language: Go (go)

or

var a, b, c = 1, 2, 3
Code language: Go (go)

or

a, b, c := 1, true, "testing"
Code language: Go (go)

As you can see, it’s even possible to mix variable types during the automatic type cast assignment.

Closing comments

These common variables are at the core of what is required when creating applications in Golang.

Learn them and their usage to better understand how to create stable applications.

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments