This is the first in a series of articles exploring data types in Go. The main goal of this series of articles is to provide detailed explanations and practical examples to help you build a clear and strong understanding of the different data types in Go. All the topics will be subdivided into manageable sections, each focusing on a specific aspect of data types, to facilitate gradual learning.
There are many different data types in Go:
The two main data types include:
1. Basic(primitive) Types
This type of data is made up of :
- Numbers(int, float64)
- Strings(string)
- Boolean(bool)
- Composite Types
2. Composite Types
These include
- Arrays
- Slices
- Maps
- Structs
Other Important Types
These are other very powerful and commonly used data types.
- Function types (functions are treated as values)
- Pointer Types (used to work with memory)
- Interfaces Types(used to define a certain behavior and simplifying flexibility)
In this article, I will focus on basic data types, as they are fundamental to understanding Go and provide a solid foundation for more complex types. While the other types, such as composite, are not necessarily difficult, they involve broader concepts and all depend on the basic types. For a better understanding of each, I cover each in greater depth in separate, dedicated articles.
Basic(primitive) Types
Let's delve directly into the details:
Numbers.
Numbers are divided into :
Integers
These are whole numbers. In Go, we use them in two main ways :
Int - This is the most commonly used, including both positive and negative whole numbers.
Uint - Used to represent only positive whole numbers.
Floating-point
These are real numbers (numbers with decimal points). They include both positive and negative numbers.
var age int = 25 // 25 is an integer
var height float32 = 186.8 // 186.8 is a float
In Go, Floating-point can either be float32 or float64. The choice lies in the level of precision you need.
Strings
In Go, strings are immutable sequences of UTF-8 encoded bytes, meaning once created, they cannot be altered, which ensures data integrity and consistency.
Immutability
Once a string is created, it cannot be changed. Any attempt to change it creates a new string.
Indexing
The individual bytes can be accessed with a given index:
s := "Hello"
fmt.Println(s[0]) // 72 (byte value of 'H')
This will print 72 as the strings store bytes and H is equal to the ASCII (byte) code of 72.
Length
The number of bytes, not characters, is returned by the len() function:
fmt.Println(len("Hello")) // 5
Unicode and Runes
As the strings are UTF-8, some characters can occupy more than one byte. to act on characters (runes), use:
for _, r := range "Hello" {
fmt.Println(r)
}
output
72
101
108
108
111
String Concatenation
The + operator can be used to add strings:
full := "Hello " + "World" // Hello World
To Byte Slice.
You can convert a string to a slice of bytes:
b := []byte("Hello")
Boolean
In Go, boolean values represent truth values; they can only be written as either true or false. They are used mostly for comparison purposes and control a program flow in if, for, or switch statements. In Go, by default, an uninitialized boolean has the value false, making it predictable and easy to work with in decision-making logic.
The section has links to the data types used so far. A link to the next article will be added once itβs ready. When a link is not given on a given type of data, simply means that it is still in the process and it will be given shortly.
Maps
Additional learning materials.
Go Documentation
W3schools
Top comments (0)