In this blog, you are going to learn Golang basics 🫡. If you are a pure beginner in Golang but know a few basic programming skills then Golang will be a real cake work for you.
In this article, I will be sharing Golang's basic types and inbuilt data structures which is a basic building block of any programming language.
Without further delay let's get into that...
Let's start with the lexers. So like other programming languages Golang has its own lexers which continuously check the grammar of its language it is correct or not where to put semicolons ( ; ).
There are 6 types of basic types i.e.
string,
bool,
int,
float,
complex,
rune
I am going to explain a little about what the types are going to do, but if you have basic programming knowledge of any strong types of language like C++, JAVA, or Typescript, you can simply skip this part.
String:
As the texts are popular structures in programming we store texts in strings in Golang in the following way listed below...
package main func main() { var name string name = "Diganta🫡" // using walrus operator name2 := "Something..." var name3 = "Another way😒" }
bool:
bool or boolean is another fundamental type in Golang. It can store only True or False. It is present in almost every major programming language out there.
package main func main() { var flag bool flag = true // using walrus operator flag2 := false var falg3 = true }
int:
Int in Golang has different sizes i.e. 8,16,32,64 and there are present unsigned ranges which means that will store from 0 to the specified ranges
package main func main() { var a int8 = 256 // It will give error because Out of the range of Int8 ranges x := 288 // It will automatically assign data type based on the system you are using... var y = 1020 }
float:
In float also we store numbers but in this, we can store decimals. Float32 and Float64 are present in float datatypes. Float64 takes more precision. The implementation is the same below I am going to explain
package main import "fmt" func main() { var myFloat float32 = 9.1664354541221 myFloat2 := 4.5588 var myFloat3 = 6.855 }
complex:
In complex type, we can store complex numbers iota. This is rarely used in the Golang domain so we can ignore this type.
rune:
In Golang we can store outside the 255 (ASCII) recognized char by using a rune. The example is listed below.
package main func main() { rune1 := 'B' rune2 := 'g' rune3 := '\a' }
Apart from that, we have few data structures in Golang i.e.
Arrays / Slices
Maps
Structs
Now I am going to explain one by one...
Arrays / Slices:
Arrays are basic data structures. In Golang normal arrays are fixed in size while defining we need to provide the
size
while defining.package main func main() { var arr [4]int = [4]int{20, 30, 40, 1} arr2 := [4]int{100,200,300,400} }
Slices are basically Arraylists from Java they just store variable size array. It means we can store elements in an array without thinking about the array size.
package main func main() { var arr []int = []int{} arr2 := []int{} }
Append method in Golang:
We can append the elements to the array/slice by using the append method which basically creates a new slice as in Golang slice is immutable.
func sliceExample() { var arr = []int{10} arr = append(arr, 100, 200) fmt.Println(arr) // 10,100,200 }
We can get a specific portion of the slice/array by using a Python-like slicing structure which is array[start: end]. start is inclusive and the end is exclusive
func sliceExample() { array := []int{10, 20, 30, 40} fmt.Println(array[1:3]) // 20, 30 }
If we pass end number greater than the slice length then it will throw an error. To check the length we use
len()
function. If we pass an array through a function args then it will pass by reference or that will pass the reference/memory address of the slice/array.
Maps:
Maps are a key value data structure which primarily used for faster search and get the value of searched value but it is not a sequential data structure means you can't traverse it just like arrays the memory address is not sequential, you will need the key to get the value. If we talk about Time complexity the searching, inserting and removing are O(1) operations.
we use comma-ok syntax.func mapsExample() { var myMap = make(map[string]int) myMap["BERLIN"] = 1 myMap["DELHI"] = 2 // To access this we will have to do this fmt.Println(myMap["DELHI"]) // 2 // TO CHECK "DELHI" PRESENT WE CAN USE COMMA ERROR SYNTAX val, ok := myMap["DELHI"] if ok { fmt.Println(val) // THIS WILL PRINT AS "DELHI" is there } for key, value := range myMap { fmt.Println(key, value) // IT WILL PRINT key and value } // To delete any key from myMap we can use delete function delete(myMap, "BERLIN") fmt.Println(myMap) // only Delhi remains // Even we can modify the map value by using the same value and assigning to somthing else myMap["DELHI"] = 1000 }
Structs:
structs are just custom-made data structures which have popularly used cases in creating web applications / APIs. We can use this as a similar structure in JSON to send and receive . Basically, we can store whatever we want in it.
type User struct {
UserName string
Email string
Age int64
isActive bool
}
In this above we are creating User structure which by itself has no meaning until we use it, Let us use it
func main() {
taylor := User{}
// we can assign the value like this
taylor.Age = 29
taylor.isActive = true
}
type User struct {
UserName string
Email string
Age int64
isActive bool
}
There are many use cases and advanced things you can do with struct, In upcoming blogs I will cover that as well.