Subha Chanda.

Understanding Basic Datatypes in Go Programming Language

Published On

Dive into the essentials of Go programming with our comprehensive guide on basic datatypes. Learn how integers, strings, booleans, and more form the building blocks of your Go applications.

← Back to blog

Understanding Basic Datatypes in Go Programming Language

This article will discuss the basic datatypes and functions available in Go.

Literals in Go

Go refers to a number, character, or string as a literal. There are four types of literal, though another rare type is available. But it isn’t used very often, that is the Complex type.

Go Datatypes.png

The four literal types are:

All the datatypes available in Go fall under these five categories.

Zero Value

Go, by default, assigns a zero value to any variable declared but not assigned with a value. This explicit zero value makes the code cleaner and removes any unwanted bug that often happens in programming languages like C or C++ due to garbage value.

Declaring Variables in Go

Using var Keyword

Go provides multiple ways of defining a variable. The first way of defining a variable is using the var keyword. When using the var keyword, the most common way of declaring a variable contains the following structure:

var keywordexplicit typeassignment

An example of this would be var a int = 911. You can drop the assignment part if you want to assign zero value to the variable. Declaring var a int will also work.

And if you already know the variable value and the variable type will not change in the future, then you can also declare the variable by removing the type. So, instead of adding the explicit datatype at the end, you can also write var a = 911.

Using the var keyword, you can also declare multiple variables at once. For example, if you want to declare variables a and b at once, you can declare them by writing var a, b int = 911, 912. All the properties discussed above also apply when defining multiple variables.

By wrapping them inside a declaration list, you can also declare multiple variables simultaneously. Here’s an example of declaring variables inside the declaration list.

var (
        a int
        b string = "Hello"
        c bool   = true
    )

Using :=

Another way of declaring variables in Golang is by using the := operator. The := operator uses type inference. When you declare a variable without specifying the explicit type of the variable, the type is inferred from the value defined on the right-hand side of the declaration. So, defining a variable similar to a := 10 will set the type of a to an integer. The Go basics chapter has a great example of this.

You can also declare multiple variables at once using the := operator in the following way:

// Declare a single variable
a := 10

// or, declare multiple variables
a, b := 10, "hello"

🚨 Though, when using the := operator, you must remember that this operator is not valid outside a function scope.

Now that we know how the variables can be declared in Go let’s move on to understanding different datatypes.

Datatypes in Go

Booleans

Booleans in Golang are represented with bool type. The bool type variables can either have true or false values. The zero value for booleans is false.

var isTrue bool = true

// or

var isTrue = true

// or

isTrue := false

The different ways of declaring a boolean datatype are shown above.

Numeric

If you come from a JavaScript background, numeric types may overwhelm you. Go has many different numeric types grouped into three categories.

go numerics.drawio.png

TypeRange
int8-128 to 127
int16-32768 to 32767
int32-2147483648 to 2147483647
int64-9223372036854775808 to -9223372036854775807
uini80 to 255
uint160 to 65536
uint360 to 4294967295
uint640 to 18446744073709551615
bytesimilar to uint8
intsimilar to int32 in 32-bit CPU and similar to int64 in 64-bit CPU
uinteither 32-bit or 64 bit depending on the CPU
runesimilar to int32 but represents Unicode code points
uintptrinteger representation of memory address
package main

import (
    "fmt"
)

func main() {
    var str string
    str = "Hello World"
    fmt.Println(str)
    fmt.Printf("Type of str is %T", str)
}

Run code here.

Output:

Hello World
Type of str is string

Conclusion

In this article, we explored the basic data types that Go offers and looked at various ways to declare variables in Go. We covered different types of data types, including numbers, strings, and booleans, explaining how each one works and how to use them in your Go programs. Understanding these fundamental types is key to building effective Go applications.

Connect with me on Twitter or LinkedIn.