Tech Stack Walkthrough

Introduction to Golang Part 3

Summary

In this video, I explain Go's data structures and core concepts. I cover arrays and slices, demonstrating their differences and use cases. I show how to work with loops in Go, emphasizing its simplicity with the single for-loop construct. I discuss maps as key-value data structures and explain package organization, including the importance of the main package and function. Finally, I delve into pointers, providing a mental model for understanding memory management through stack versus heap, and when to use references versus values.

Transcript

To work with a sequence of data in Go, we have two options. We can either use an array or a slice. An array is fixed in size. So it can only hold as many elements as you tell it to hold. So it will pre allocate space for, let's say five elements. Whereas a slice, you can add as many elements as you want because it's dynamic. So it will keep up, keep on growing until it reads a maximum capacity that's specified in the language. And then it will be reallocated, but from your point of view, it will just keep on growing. So you're most likely going to be using slices. But if you know up front, the size of your, let's say, array or the elements you're going to be working with and an array will be more efficient. And so the way you define these ones is, is pretty straightforward. So you do like, well, it's array here. Where this one can hold two entries of, of wallet. And if we were to do slice, we will simply just say wallets, let's say slice, and then we don't specify the, the maximum capacity. And this one here is the option that you would most likely use. So if you're working with a sequence of data in Go, this is the two options you have. This is one of my favorite examples of the simplicity of Go. There's only one way to do loops, which is the for loop. And the for loop looks something like this. So we specify the keyword for an index value, and then we set it equal to something we can iterate over, which we can take our wallets away from earlier. So here we have the index and the value. Now, we don't need to necessarily specify both of these. We can, as earlier, you can ignore the index, or we can only use the index if you just want to index into our iterable, our array, our slice, whatever. But this is the basic form of a for loop. We can also loop over a predetermined amount. So let's say we do something like this, start value is going to be zero, and then as long as the i variable is lower than, let's say, 20, and we're going to increase the value of i for each iteration, and then do something. This is another way of doing loops, but you can only use default loops to do loops in Go. We also have built in support for maps in Go, which is a key value data structure. One example of when this is useful is a situation where you have something you want to group on a specific key. An example of this could be, let's say, cafes in cities. So you can create a zero valued maps by doing the var keyword. And let's say cafes in cities. This is a very random example. And then we specify the map keyword. And then we specify the data type of the key. So in this case, it's just going to be a string. And since we want to specify multiple keywords, cafes in whatever city we are, we are looking for, we're going to have a slice of strings as well. Right now we can loop over this just as we did earlier saying this case key value range cafes in cities. So this way you can get into each city and then find each cafe in the city. In Go we group related logic and functionality into a package like we saw earlier with our private versus public example. You can have as many packages as you want, but no matter the amount of packages that you have, you must have an entry point. The program must have an entry point, which is the package main. And that package main must have a main function. This is the function that you might have seen we've been using to write all of these examples in. Now, you can have multiple main function or main packages in your project, but they must be in their own directory. So a common approach to see you see in Go is that let's say you have like the main app and then you have an admin, maybe you have some tooling, you will divide them into sub directories. So you can have something like commands app main.go, or you can have something like commands admin main.go. The command slash, the name is a, is more convention. It's not a, it's not a requirement, but having this set up, we can now, you built different binaries based on the same code that we have in our project. So what we talked earlier about the compiler, Go is a compiled language, so it compiles down to a single binary. And having this structure, we can now use dependency injection, which is basically just saying this thing right here needs these things over here. So we provide those things and then we can build that. So this is a way of, of managing different parts of our, of our Go applications. In Go, we have the concept of pointers. This is a bit more advanced. So if you are familiar with this topic, you will probably feel the section is a bit slim, but it's not. The goal here is to provide a broad overview, a mental model of the concept. So the way we point towards something in Go is to use the Amazon. So let's say we have a variable here called, a variable, we have a struct here called largeWallet. We will point to this by saying largePointer to largeWallet and then largeWallet. If you took the time to look up stack versus heap in the beginning, you're sort of familiar, probably sort of familiar what's happening here. But when we say we declare a variable or function, it must Live somewhere, must be stored somewhere, and that somewhere is memory. You can think of stack versus heap as sort of like long term versus short term memory. The stack stores things in a more permanent way in the sense that the size of the stored item must be known up front. It cannot change, and it's allocated and deallocated automatically. While heap stores data in a more flexible way and we'll dynamically allocate and deallocate during one time of our program. So the dynamic nature here can sometimes cause memory leaks and cause the program to crash. We don't have to worry about this because we have a garbage collector that takes care of this for us. So what's the point of telling you all of this? Well, we can reference things in Go by what is known either as reference or value. So if you want IP key where we have earlier, it's more efficient to point to this memory address than to allocate new space for our copy. But if you need to change the value of API key, we need to, we need to get the value out and then we pass the whole object. So to, to dereference something we would say, Dre Pointer to wallet. And we can see to wallet, right? A way of thinking about when you should point towards something and not point towards something is kind of like if you're sending a package to someone's house It's much easier to send the package to the address of the house than it is to move the house to where the package is. For most things you Not really gonna have to worry too much about it, but just know that it it will be more efficient to point towards something but sometimes you need to have the value to be able to change the actual data and You If you just have a reference, you can change it. You need to actually get the data out.

Early Access

$95 $25 USD

Get access

Invoices and receipts available for easy company reimbursement