What Are Data Types?
Data types are the building blocks of any programming language, and Go is no exception. In this blog post, we’ll take a look at the various data types that are available in Go. and how they can be used in your programs.
First, let’s start with the basic data types. Go has several built-in data types for storing numbers, strings, and Boolean values. These include: - Intger types: int, int8, int16, int32, int64 - Floating-point types: float32, float64 - Boolean type: bool - String type: string
These types can be used to store numbers, boolean values, and strings respectively.
Integer Example
We can create variables that make use of the int type like so:
var x int = 5
There is also what Go developers call the shorthand syntax which looks like this;
x := 5
The two code snippets are identical in functionality. There is a caveat to the shorthand declaration type though. You can only use this way inside of functions. So if you have variables that you know will be global you will need to use the longer way with the var
keyword.
Boolean Example
If you are new to programming then you might not be familiar with what a Boolean value is. It is one of the simplest data types to work with. A boolean value can only ever be one of two things. True
or False
. An exmaple would be:
// Using `Var` keyword
var isLightOn bool = true
// Shorthand
isAccountActive := false
// Error incorrect value
isTall = "most definiltye" // => Error wrong value
As you can see with the examples the value can only be true or false. If you try to put a different value in it will result in an error.
Floating-point Example
Unlike JavaScript Go has both integer values or whole numbers as well as fractional values or decimals. These values are known as floating-points and can be used like the following:
// length of a rectangle
l := 12.7
// width of a rectangle
var w float32 = 4.2
Go automatically defaults to float32. So the shorthand variable l
is of float32 even though we didn’t explicitly set it. The w
value we set to float32 explicitly. If you find yourself needing more precision then you can explicitly declare the variable as float64. I believe that is a very rare case but it can be done.
String example
In order to use textual values in Go you will need to use the string data type. That can be done in the following way:
name := "Jonathan"
Pretty simple. You just need to wrap the value of the variable in dobule quotes. If you are coming from languages like Python or JavaScript where you can use either the single quote '
or the double quote "
characters to declare string types you will need to break that habit. Like other compiled languages Go treats single and double quote characters differently. We will cover the use case for the single quote later. Just keep in mind that double quotes is what you should be using for strings.
Other Data Types
In addition to these basic data types, Go also has several other types that are useful for specific purposes. THese Include:
Arrays: Arrays are fixed-size collections of elements of the same type. You can declare an array like this:
var a [5]int
Slices: Slices are similar to arrays, but they are dynamic in size. You can declare a slice like this:
var s []int
Maps: Maps are a key-value data structure that allows you to store and retrieve values by a unique key. You can declare a map like this:
var m map[string]int
Structs: Structs are custom data types that allow you to group togeter multiple fields of differnet types. You can declare a struct like this:
type Person struct { Name string Age int }
Pointers
Go also has support for pointers, which allow you to reference memory locations directly. Pointers can be declared like this:
var x int = 5
var p *int = &x
We will go more in depth with Pointers in another article.
Conclusion
Go provides a wide variety of data types to work with, from basic types like integers and strings, to more advanced types like arrays, slices, maps, structs and pointers. Understanding the capabilities and limitations of each data type is crucial to writing efficient and effective Go programs.