Golang — Zero to Map (part 1 of a Zero to Hero series)
Introduction
Golang is a language created by Google to be performative and easy to use and write.
I’ve been in contact with this language for the past couple of months, and I figured out that the best way to fix the knowledge will be helping other people to improve their Golang skills. In this article, we’ll learn the basics about Go, like types, variables, functions, advanced data structures, and how to iterate them.
I hope that this article can be helpful to you.
Let’s start talking about Golang types.
Types in Golang
bool
→ boolean type. Can betrue
orfalse
;string
→ set of characters;int
anduint
→ types machine-specific. Can be 32 bit/4 bytes in 32-bit machines or 64 bit/8 bytes in 64-bit machines;uint
→ unsigned integer. The difference between the "common integer" and the "unsigned integer" is that the unsigned integer only accepts positive numbers;- When an integer has a number after the keyword —
int8
,int16
,int32
,int64
,uint8
,uint16
,uint32
,uint64
—, the number represents the variable's size (in bits) created; byte
→ alias forint8
;rune
→ alias forint32
. Rune represents a Unicode code point.float32
→ float number 32-bits sized;float64
→ float number 64-bits sized;complex64
andcomplex128
→ Are complex numbers built using real and imaginary numbers (32-bit and 64-bit, respectively).
Variables and constants
Variables can be declared using the keyword var
or the short assignment statement:=
.
Constants can be declared using the keyword const
, followed by the variable's name and type.
Zero values
Zero values are:
0
for numeric types;false
for boolean types;""
forstrings
;nil
for amap
, achannel
, aslice
, etc.
Example
Loops
You can use the classic for loop:
Golang doesn’t have a while
keyword, so to create while loops, you can do the:
You can also create an infinite loop:
Example
If Else
It’s like other languages
Example
Conditional Statement — Switch
Unlike other languages (like JavaScript, Java, and C), on Go, the executed that will case on the switch statement is the selected case (so, you don’t need to use the break
keyword to stop the execution. This will happen automatically). Switch cases execute from top to bottom, and the execution will stop when one case succeeds.
Switches without conditions are the same that a switch with true. It’s cleaner than using a bunch of if-then-else.
Example
Arrays
Array types are declared as [n]T
on Go, where T represents the type and n represents the size of the array. var a [10]int
will declare a variable called a that has as type an array of integers with ten values.
The built-in method len(a)
— the argument passed will be the array to be checked — will return the array's length.
Example
Slices
Slices as an array is a sequence of values, but an array has a fixed number of values, but the Slice is dynamically-sized, and because of this, slices are more common than arrays. Unlike arrays, slices are typed only by the elements they contain (not the number of factors).
The type []T represents a slice of elements with type T.
An array can form a slice by selecting two bounds of the array, like on the following code: a[low: high]
.
This code will select the low index and exclude the high index. For example, doing a[1, 5]
from an array will create a slice from 1 to 4, in other words, an array with three positions.
We can also create an empty slice using
This code will create a slice of int with a capacity of 5 elements.
- len() returns the length of Slice (no of filled items)
- cap() returns the full capability of this Slice (because the Slice is dynamic, so the size increases when you put new items, and the size of items will double each time that you fill the actual maximum size)
Instead of just basic operations, slices also support several functions to get richer than the array:
- append() built-in slice function that adds more elements to the existing Slice. This method will return another slice with the addictions without change the original Slice, so the only way to update the Slice is by reassigning
- copy() built-in method to create a copy of a slice
Example
Slices are like references to arrays.
- Slices don’t store data. It just describes a section of an underlying array.
- Changing the elements of slices will also change the array they are pointing out.
- Those changes will also affect other slices sharing the same underlying array.
Examples
Slice Literals
You can also declare slice literals. It’s like the array declaration but without a pre-determined size.
Slice defaults
Using the default limits, you can omit the high or low bounds of your slice size with slices. The default for the low bound is 0, and the Slice length is the higher bound.
With an array, you will do
With slices, all those expressions are equivalent
Example
Slice length and capacity
- Slices have both a length and a capacity;
- The length of a slice is the number of elements that this Slice contains;
- The capacity of the Slice is the number of elements on the underlying array, starting from the first slice element;
- You can extend or shrink a slice’s length by re-slicing the Slice.
Example
Nil slices
A nil slice hasn’t an underlying array, and its length and capacity are 0.
Examples
Maps
Maps are a built-in associative data type. It’s like Map in Java and is a structure that maps keys and values.
As I said in part 1 of this series, the zero value of a Map is nil
.
Empty Map
To create an empty map, you can use the make
built-in method:
If you get a value from a key that isn’t declared, his value will be zero, related to the type of your map value.
Mutating Maps
You can insert or update an element in the Map
You can pick up the element
Delete the element
With two value assignments, you can also check if a value exists on a map
If the value is in key, the element
variable will get the value of this element, and the ok
variable will be true
.
If the value isn’t in the key, the element
variable will be zero (related to the specific type), and the ok
variable will be false
.
Declaring inline Map
It’s also possible to declare a non-empty Map with filled keys on his creation.
Examples
Conclusion
I hope that this content has been clear to you. Feel free to reach out with any doubts, and connect on LinkedIn.
See you in the next article! In the next article, we will learn more about how to iterate these built-in structures (map, slices, and arrays and deep down on more Golang features :)
Stay safe :)