Golang Pointers - Fully Understanding Pointers in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
pointers and memory in general are difficult concepts to grasp in today's tutorial we're gonna first show you an example of the golang pointers and then we're gonna dive right into the underlying mechanism of a wider needed and how they really work under the hood so you can fully understand pointers and go [Music] what is up ninjas and welcome to the golang dojo this channel is all about the gold programming language so if you want to learn the golang and become equivalent ninja you know what to do now here we have a half a finish example on the golan so we have a variable named wallace that's me that is a string variable that has a content of uses a ninja star print out that a consonant hopefully this is going to print out uses a ninja star and then we're going to call a function that we have already wrote and that is going to take a string as a parameter and attempt to change that parameters content into uses a ninja sword and then we're going to print out wallets again so if we actually run the program unfortunately it's actually not going to change the variable wallets is to use this ninja sword and that is because we need to declare a slightly different function let's call it a change gear with a pointer change the content of this a pointer and change that to uses a ninja sort and if we go ahead and call this function and when we are passing in wallace as a parameter we are actually passing in the address of the wall reserve variable and then we're going to print out wallets again so if we go ahead and run this program it is going to change the content of the wall is available from uses a ninja star to uses a ninja sword and now let's talk about how all of these really work under the hood now when the computer starts off it is a granted a certain amount of memory space of operation out of the memory space are granted it is divided into quite a few sections two of which are going to be the stag and the heap what we want to pay our attention to in our example in particular is going to be the stack so if we actually imagine this as our stack the stack would be responsible for storing any runtime functions as well as any local variables local variables would be wallets in this case and any of the functions being called would be changed here as well as a change here pointer here now let's imagine this being the stack again this is the stack when the main function is being called it is going to be pushed onto the stack saying that this is at the start of the main function and then we're going to have our first variable that is going to be the wallace is which is a string variable next we are going to call the function call change a gear so this will be the start of the change in gear function within this function we're initiating another parameter being passed in and that is the wallet's copy variable these two are completely different pieces of a data on the stack so when we are done with the change gear function we're going to pop these two data out of the stack so in turn this will look something like this is also the stack but in a later time frame this will have at the start of the main function still because we are not done with the main function and we're still going to have the wallace variable however these two are going to be part of the stack as you can see the wall is copied here and the according changes or any values assigned into the wallace copy is not going to be reflected back into the wireless variable inside of the main function in order to resolve that dilemma we need to utilize the pointers and addresses now remember that in the main function we have a variable called wallace which is a string variable thus a variable contains two things that we care about in particular the very first one is going to be the string content of the variable uses a ninja star to be clear this is not exactly how a string variable is being stored in memory but i'm simplifying things so that it is a little bit easier to understand conceptually so this is the content of the variable wallace and the second thing that we need to pay our attention to is going to be the address of this variable we call it one two three now this is also not exactly how addresses look like but i'm again simplifying things so that it is a little bit easier to understand conceptually so this is going to indicate where this variable wallet lives inside of the memory now if we go back to the function here inside of this function we are actually creating a copy of this wall as a pointer here so how the wireless pointer looks like in the memory would be something like this so the wallace pointer also contains two things that we care about in particular the very first one is this piece of a data the wallace pointer variable also has a address otherwise we won't be able to find it inside of the memory space we'll just use one two five but instead of storing a string content whether there's a wireless pointer is going to store inside of this container here is the address of the wall is a variable something like this and if we go back to the code we are actually passing in the address of the wallet variable which is one two three in this case now when we are using the star operator we are telling the program that we don't want to change the content of this variable wallet's pointer we want to change the address where this pointer is pointing to which is the wallace variable here using the star operator which is a dereferencing operator saying that we actually want to change the content of the variable where this wall is a pointer is appointing to so instead of changing the address here to something different maybe i don't know like maybe it's one two seven here we're not doing that we're going to the address of the wallace variable change the content to uses a ninja sword instead so instead of a ninja star change that to uses a ninja sword so this is how we can change the content of this variable even though it is a passed into a different function in go now to be completely frank the star operator is somewhat confusing because we're actually overloading the functionality of the star operator first we're actually using it as a indicator that this is not a string variable but a pointer variable as you can see here the second functionality is using it as a dereferencing operator saying that we don't want to change the variable was a pointer where we want to de-reference that and change the content of the variable where this wallet pointer is pointing to so this is how pointers and addresses work in code as well as how stack and memory work in general to be 100 frankly these are quite difficult concepts to grasp especially for beginners in programming so if you still have any confusions any questions feel free to leave them in the comment section down below i'll try my best to answer all of your questions you
Info
Channel: Golang Dojo
Views: 1,844
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, golang basics, golang basic pointers, golang pointers, pointers in golang, pointers in go programming languages, fully understanding pointers in go, pointers and memory in golang, pointers in golang explained, pointers in golang tutorial, pointers in go, golang memory
Id: XOz_Xu5WanQ
Channel Id: undefined
Length: 8min 16sec (496 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.