• Home
  • Blog
  • Variables and Primitive Types in Go

Variables and Primitive Types in Go

This article was published on Apr 22, 2022, and takes approximately 2 minutes to read.

Variables

While in JavaScript, we have var, let, const, and often people get confused about the difference between those three and when/which to use each of them, in Go, it seems things are very straightforward.

For variables, we'll always use the var keyword and const for constants:

package main

import "fmt"

const POINTS = 10 // Global constant
var name string   // Global variable

func main() {
	const COUNTRY = "Brasil" // Local constant
	var age = 30             // Local variable

	name = "Raul"

	fmt.Println("Hello", name, "you are", age, "years old")
	fmt.Println("Your rate is", POINTS, "points.", "You are from", COUNTRY)

	name = "John"
	age = 33

	fmt.Println("Have you changed your name?", name, "you are", age, "years old")
}

It's almost the same concept as we cannot reassign a value to a constant, but we can do that with a variable.

The difference in reassigning a variable value is that because we said `name` is type of string, we'll never be able to assign a non-string value to it as we can in JavaScript.

Because Go is a typed language, in some cases, we have to declare its type, as we did for name but in others, Go can infer based on the value we're assigning it to, like the age variable.

Even for cases like Go can infer, we can make the type explicit if we want to:

package main

func main() {
	var age int = 30
}

Short Variable Declaration

There's another way of declaring a variable that's commonly used in Go codebases which are using the operator :=:

package main

import "fmt"

func main() {
	var name = "Raul"
	age := 30 // typed short handed variable

	fmt.Println("Hello", name, "you are", age)
}

We can simply omit the keyword var by declaring it like this.

It's important to mention that this shorthand will only work for variables with an initial value. If it's an unassigned variable, we must use the var keyword anyways.

Extra: checking variable type

As I said in the first code in Go, we don't have globals in Go as we have in JavaScript. That means operators like "typeof" are not straight available to be called.

Instead, we need to use a package called reflect for this case. This package does much more than only checking in runtime types, but for now, it's what we need for:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	name := "Raul"
	age := 30

	fmt.Println("Hello", name, "you are", age)                                                // Hello Raul you are 30
	fmt.Println("name is typeof", reflect.TypeOf(name), "Age is typeof", reflect.TypeOf(age)) // name is typeof string Age is typeof int
}

Primitive types

As in every language, we can start talking about types with the primitive ones.

package main

import "fmt"

func main() {
	var name string
	var age int
	var basicPi float32
	var complexPi float64
	var isAdult bool

	name = "Raul"
	age = 30
	basicPi = 3.14
	complexPi = 3.141592653589793238462643383
	isAdult = true

	fmt.Println(name, age, basicPi, complexPi, isAdult)
}

There are only 2 significant differences between JS/TS and Go regarding types. They are:

1. Number

Instead of only number, in Go, we have int for non-point floating numbers, float32 for 32 bits floating-point numbers, and float64 for large floating-point numbers.

Side note: if we declare a floating-point number without saying it's float32 or float64, Go will always infer that its type is float64 for safety.

2. Booleans

In JS/TS, the keyword is boolean while in Go it's bool

Conclusion

This post was only an introduction to this matter. Later, I'm going to dive deep into more complex types and nuances and standards for variables creation that I already notice are very different from JS.

References