Go is a statically-typed programming language that provides a rich set of data types for representing different kinds of values.
We are going to discuss all the data types that are available in go. We shall deeply discuss each, show examples and ensure we have covered them from the most basic view point.
To learn about datatypes in go, it is important that we have some little experience from another programming language for example python, js and others. Knowing a programming language will make it easier learning and grasping the terms used in go.
And we shall begin.
1. Boolean
Boolean is a data type is used to represent true or false values. In Go, the Boolean type is represented by the keyword bool. The two possible values for a Boolean variable are true and false.
Here is an example
var isCloudy bool = true
In our example we are using the keyword var to show its a variable, the bool keyword to show that the datatype is a boolean and the value which is true for our case. This shows that the initial value of the isCloudy variable is true.
2. Integer types
Integer datatype is represented by a int keyword.
It is used to represent whole numbers whether positive or negatives.
Here is an example;
var amount int = 121
In the above sample code, we declare an integer variable called amount and initialize it with the value 121. We use the int type to represent this value.
3. Floating-point types
The floating data type is represented by the float keyword.
Float represents decimals whether positive or negative.
Here is an example:
var price float = 3.99
In the above sample code, we declare a floating-point variable called price and initialize it with the value 3.99. We use the float type to represent this value.
4. String
string keyword is used to represent strings.
Strings are used to represent any textual for of data. .In Go, strings are represented as a sequence of characters enclosed in double quotes.
Here is an example:
var name string = "Bob"
In the above sample code, we declare a string variable called name and initialize it with the value "Bob". This variable could be used to represent a person's name.
5. Array
- used to represent a fixed size collection of values.
The array data type is used to represent a fixed-size collection of values of the same type.
In Go, arrays are declared with a specific size and the
elements are accessed by index.
Here is an example.
var numbers [3]int = [3]int{1, 2, 3}
In the above sample code, we declare an array called numbers with a size of 3 and initialize it with the values 1, 2 and 3. We use the [3]int type to represent this array.
An array syntax has: the var keyword, the name of the variable, the size of the variable(enclosed in a square bracket[]) an equal sign and the values if need be. It as well has the int datatype which means all the values stored here are integers. we cannot add strings to this array.
6. Slice
Slice is like an array but without the size.
The slice data type is used to represent a variable-size collection of values of the same type.
In Go, slices are similar to arrays, but they do not have a fixed size and can be resized dynamically.
Slices are declared using the same syntax as arrays, but without specifying a size.
var names []string = []string{"Ben", "Anna", "Jack"}
In the above sample code, we declare a slice called names and initialize it with the values "Ben", "Anna", and "Jack". We use the []string type to represent this slice.
In this case we have not used any number to show the size. The keyword used here is string which ensures we only keep strings in our slice.
7. Map
Key- value pairing.
The map data type is used to represent an unordered collection of key-value pairs.
In Go, maps are declared using the map keyword and the types of the keys and values.
Here is an example:
var ages map[string]int = map[string]int{"Alice": 42, "Bob": 37}
In the above sample code, we declare a map called ages with string keys and integer values, and initialize it with the key-value pairs "Alice": 42 and "Bob": 37.
We have var as the variable definition, ages as the name of the variable, map keyword to show that we are going to have key and value, the square brackets with string to show that the key will be a string and the int outside the square brackets showing that the value will be an int.
On the right we have examples as defined , following how our map was defined. We start with the map keyword, a square bracket with the string inside it and int outside it. we now have an opening and closing bracket with out key-value examples inside it.
######
So far we have been storing only same type of data types together and gets an error when a different datatype is used. For example if we were storing strings and we add an int, we get an error.
What happens when we want to store different data types, for example string and int together.
Thats where struct comes in.
We are now going to learn about struct.
8. Struct
The struct data type is used to represent a collection of fields of different types.
In Go, structs are declared using the type and struct keywords.
Here is an example:
type Person struct {
name string
age int
}
var alice Person = Person{name: "Alice", age: 42}
In our example above, we declare a struct called Person with two fields: name of type string and age of type int.
We then declare a variable called alice of type Person and initialize it with the values "Alice" and 42.
In our example, to define the struct, we have used the type keyword then we pass the name of our struct, we then use the struct keyword, we open and close brackets, in there we pass the types our struct has to have, in our example, it has a string and int.
we later then call the name of the struct we had initially called our struct and pass values to it.
Let's discuss more here:
https://github.com/kibetamos/booking-app
Cheers and Happy coding.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more