Array & Slice in GoLang

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
Search for a command to run...

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
No comments yet. Be the first to comment.
In this series, I'll be posting articles to get you started with Google's GoLang.
In this post, I'll walk you through setting up your go project. As a prerequisite you must have go installed in your system, you can get it for your system from https://go.dev/dl/ Now that that's out of the way, go ahead and create a directory called...
Migrating a production database is often a high-stakes operation. Traditional "dump and restore" methods require significant maintenance windows, leading to service downtime. To solve this, PostgreSQL offers Logical Replication, a native mechanism th...

Ever found yourself in the endless cycle of "delete Docker image, redeploy, repeat" to keep your Dokku apps updated? You’re not alone. If your Dokku-deployed application relies directly on a Docker image (perhaps from Docker Hub or your private regis...

Lately, my internet has been throwing tantrums — not because the connection itself is bad, but because every time the power blinks, my router decides it's time for a little nap. Naturally, the obvious

So, one day I saw my old laptop sitting there, collecting dust like it was auditioning for a role in an apocalypse movie. And then it hit me: "Why not turn it into a server?" There was just one minor hiccup. Its Ethernet port was as dead as my dreams...

Recently I was tasked with setting up kubernetes cluster with an Nginx Ingress. As a newcomer to Kubernetes, I tackled this setup and learned a thing or two along the way. Let me walk you through my experience and share some helpful tips. The Goal: S...

Let's say we are working on a go app and we have to store the data of 2 users, so far we know about variables and how they can be used for data, let's say we create two variables and store the name of our 2 users. But then we receive a request to add another user well, we can create another variable for our new user. But you can see this is becoming a problem. Our application is not scalable and the code will also start to become a mess after a certain number of users. How do we get around this problem?
Meet Arrays and Slices.
An array is also like a variable it has an identifier and a data type. But unlike a variable we can store data many different predefined numbers of elements.
Let's say for 100 users, we create an array:
var users = [100]string{} // an empty string array of 100 elements
When creating an array we need to know its size beforehand (how many elements it can hold).
First, we're defining a variable users which holds the memory address for the array, after the = sign we're defining the size of the array and then the type of data this array will hold, and finally the {} is the initial value of the array, which is currently empty.
You can also add initial values by providing them in the {} separated by a comma.
var users = [100]string{"Vivek", "Harsh", "Kartik"} // an string array with 3 initial value
How about accessing and storing the data?
var users = [50]string{"Vivek", "Harsh", "Kartik"}
users[3] = "Sachin" // storing the user at 3rd index
fmt.Println(users[0]) // this will print "Vivek"
fmt.Println(users[1]) // this will print "Harsh"
users[0] = "Vivek Kaushik" // Reassign the value at index 0
fmt.Println(users[1]) // Will print "Vivek Kaushik"
fmt.Printf("length of the array: %v\n", len(users)) // Will print "length of the array: 100"
Arrays have something called as Index. Think of the array as a box with compartments, and each compartment has a number assigned to it (index). The number starts from 0 to the length of the array minus 1. So for our previous user example, we will have index from 0 to 99. We can use these indexes to access, assign or reassign values.
If we try to access a value from an index that is out of range, from our previous example fmt.Println(users[100]) this will give us an Index out of bound error as we only had index up to 99.
Note:
len()is another function, that gives us the size of the array.
Arrays are great but they are not always useful, as most of the time we do not know how many elements we will be adding. This is where Slices comes in.
For slices we don't have to define size, they are dynamic and can grow and shrink as needed.
var users = []string{} // empty slice of 0 length
fmt.Printf("length of the slice: %v\n", len(users)) // Will print "length of the slice: 0"
users = append(users, "Vivek")
fmt.Printf("length of the slice: %v\n", len(users)) // Will print "length of the slice: 1"
fmt.Println(users[0]) // this will print "Vivek"
First, we initialize an empty slice which is very similar to an array except we don't specify the size of it. Next we print the size of our newly defined slice using the len function which gives us zero. Then we use another function called append which takes our slice and a value and appends it to the end of slice. After using the len function again we see that the size is now 1. Finally we print the value at index 0.