Understanding Basic Datatypes in Go Programming Language
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.
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.
The four literal types are:
- Integer
- Floating Point
- String
- Rune
- Complex Type
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 keyword | explicit type | assignment |
---|
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.
- Integers: The integers in Go are available in four sizes. These are available as both signed and unsigned. Signed integers are represented with
int
, and the unsigned integers are represented withuint
. Along with signed and unsigned integers of different sizes, there are a few special type integers. Let’s look at all the integer types from the table below.
Type | Range |
---|---|
int8 | -128 to 127 |
int16 | -32768 to 32767 |
int32 | -2147483648 to 2147483647 |
int64 | -9223372036854775808 to -9223372036854775807 |
uini8 | 0 to 255 |
uint16 | 0 to 65536 |
uint36 | 0 to 4294967295 |
uint64 | 0 to 18446744073709551615 |
byte | similar to uint8 |
int | similar to int32 in 32-bit CPU and similar to int64 in 64-bit CPU |
uint | either 32-bit or 64 bit depending on the CPU |
rune | similar to int32 but represents Unicode code points |
uintptr | integer representation of memory address |
-
Floating Point Types: Floating point types represent floating point numbers. There are two types available in Go:
-
float32
: Range from 1.401298464324817070923729583289916131280e-45 to 3.40282346638528859811704183484516925440e+38 -
float64
: Range from 4.940656458412465441765687928682213723651e-324 to 1.797693134862315708145274237317043567981e+308Don’t worry about the ranges. You don’t have to remember them. Just know that
float64
can hold larger numbers thanfloat32
.The zero value in the case of float types is zero.
Let’s take a look at a floating point type as an example. And don’t worry about anything except the variable declaration part for now. In a future article, we’ll cover everything you see in the program here.
package main import ( "fmt" ) func main() { a := 6.254e-21 b := 4.45e7 c := b - a fmt.Println("The difference between b and a is: ", c) fmt.Printf("The Type of a, b, c are: %T %T %T", a, b, c) }
You can run the code here.
Output:
The difference between b and a is: 4.45e+07 The Type of a, b, c are: float64 float64 float64
-
-
Complex Type: Go also has a type for complex numbers. This type is rarely used, but it is good to know. But if you don’t know what a complex number is, there is a rare chance you’ll ever use it. You can read about what a complex number is from here. There are two types of complex types available in Go,
-
complex64
: It usesfloat32
types for storing the real and imaginary part -
complex128
: It usesfloat64
types for storing the real and imaginary partpackage main import ( "fmt" ) func main() { a := complex(1.8, 3) fmt.Println(a) fmt.Printf("The type of a: %T", a) }
Run the code here.
Output:
Hello World Type of str is string
-
-
String: The next data type we’ll discuss in this article is string type. But before discussing the string type, let’s discuss Unicode code points. A code point is a number assigned to represent an abstract character in a system. A code point is represented with U+NUMBER in Unicode. Where
NUMBER
is a number ranging from 0 to 10FFFF. For example, the Unicode code point U+0021 represents the exclamation mark. Thestring
data type is a sequence of Unicode code points. Defining a variable withvar string str
will explicitly set the type tostring
.
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.