Go Data Types
What is a Data Type?
A data type is a type of data that we can assign to a variable. In Go, we have a few basic data types that we can put together to make complex ones. In this tutorial, we'll be covering the most important ones.
Strings
A string is some text. If that's hard to remember, think of text as characters strung together on a string. In Go, strings are wrapped in double quotes. If we don't do that, Go will think that we are referring to a variable and we'll get an error.
Below is an example of making a string variable:
var websiteName string = "Learnmonkey"
Integers
An integer is a whole number that can be positive or negative. In Go, we simply write out the number, no quotes or anything like that. To make it negative, just add a dash in front of it. We can do arithmetic with integers.
Below is an example of making an integer variable:
var numberOfStudents int = 27
var revenue int = -100
Booleans
Booleans are either true or false. Since computers are made from ones and zeroes, which are equivalent to true and false, you'd imagine booleans are pretty important. In Go, to make a variable true, we simply type true
, all lowercase. Likewise, we type false
for false. Booleans are named after the English mathematician George Boole.
Below is an example of making a boolean variable:
var isOn bool = true
var isEmpty bool = false
Floats
Floats are an abbreviation of floating point numbers and can do all of the same thing as integers. The only difference is that floats are decimals. In Go, there are two types of floats, float32
and float64
. float64
uses 64 bytes of memory, while float32
only uses 32 bytes of memory. So, float64
can store larger floats and floats with more decimals. However, float64
uses more memory, so there is a tradeoff.
Below is an example of making a float32
and a float64
variable and constant:
const pi float32 = 3.141
var percentage float64 = 2.4563543
var bankAccountBalance float32 = -1643.95
Making Your Own Data Types
You can make our own custom complex data types using structs.