A Practical Guide to Pointers in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when I first started learning go I was pretty confused about pointers not so much about how pointers worked or what they were but more about when and why I should be using pointers so in this video I'm going to take you through some practical examples of when and why you should be using pointers okay so let's first start off by understanding what pointers are so let's pretend that this is a stick of memory now memory is very complicated but you don't really need to know how memory Works in order to understand pointers in go you just need to understand the very Basics so each one of these squares here is a block of memory and each one of these blocks is going to have its own address now this address is going to be a hexadecimal number to select this and in each one of these blocks we can store a variable so let's say that in this block here we store a variable called hello now this is just a string now there's two things that we can get out of this block either we can get this value here hello or we can get this address here so if we wanted to get the address and let's say that this is referenced by a that is the variable name and the value is hello so to get the address we would use this empath sign and we would say a and that is going to return us this address here and my arrow is back to front Okay so what happens if we wanted to get the value here of hello then of course we would just use an A okay so now let's say that we have the block of memory here and so we have this empan a and we actually want to turn that into the value where we can use this other syntax and this other syntax is going to use the asteris and we can say a so this asteris a is going to dreference the pointer and it's actually going to return the value here okay so that's the basics of the syntax and what's happening when we use pointers so the first reason that you might want to be using pointers is to pass a property by reference so let's go have a look at an example of this so in this basic program here we're declaring a variable called a and we're assigning this the value of hello so we're going to print out a and of course on this line here we're just going to get the value of hello then we're going to try modify a and we're going to try turn it into this string that says modified and then we're going to print a again so you can already guess what is probably going to happen here we're going to run go main.go and of course it doesn't change so why is this not changing well what we're doing here is we're going to print a of course that's just going to return hello then we're going to call this function modify a we're going to create a new instance of a then we're going to assign the value to modified and then we're just going to print a again now this value of a on line six here would in fact be modified we can print that out and you can see that that is modified so this is the first reason as to why you might want to be using pointers you might want to modify something inside of another function so we can easily fix this by saying a is a pointer to a string we're going to change the value of the block of memory that a is pointing to and then down the bottom here we need to say that we're going to pass in the memory address for a so let's also print a up inside this block here and then we're going to print a after we've done the modification so you can see before it's hello inside mod string we get this block of memory here and then after the value is modified so essentially what we're doing is we're saying hey modify string here's a block of memory or an address to a block of memory modify the value inside of that block and then when we reference that again the value is going to be different so a really common place that you're going to see this pointer so you can D so a really common place that you're going to see this is in HTTP Handler functions so you'll notice that the response writer here is not a pointer it's actually just a completely new value but the request here is a pointer to the request now this really confused me when I first started learning go because I didn't understand why one was a pointer and the other wasn't now the reason that the request is a pointer is because we can modify things on the request so let's say this handle Funk was actually a piece of middleware and we want to write something to the request let's say we want to write a new piece of data then we want that new piece of data to be available inside inside of every Downstream Handler and if we don't use a pointer here then it would only be available inside of this block here so another really common use case for pointers is when we want to make a property optional so let's have a look at these functions that we have here now this function is called can return nil and this is going to return a pointer to a string so we can return nil here in this function but let's say we weren't returning a pointer to a string we were just returning a string well of course now we can't return nil so the reason for this is because when we return a pointer we're returning a memory address and it doesn't necessarily have to be a valid memory address it can be nil so let's say that can return nil doesn't return a pointer it just returns a string another option here is to return the default value so that would just be an empty string okay so sometimes you're also going to see pointers to arrays of things it doesn't necessarily have to be a string it could be anything so try to avoid doing this because it's sort of redundant you don't want to return a pointer to an array you can actually just return the empty array here and I think that that makes a lot more sense than returning the pointer to an array okay so this is a more practical example of when you would use this so let's say we have a database call and that database call is inside of a function called get user by ID now that user may not exist and so if it doesn't exist we just want to return nil and we're also going to return nil for the error so if we remove the pointer here then we're going to say that you can't return nil for the user what you could do instead is return an empty user and that's going to be acceptable as well okay so one caveat that I want to add to this is if you find yourself doing this ask yourself if you really need to do this sometimes it's better to return the default value instead of nil for a function and the reason for that is because you're then not going to reference nil pointers so I can give you an example of this let's say that get user by ID is returning nil because we haven't found the user then we can say user is equal to get user get user by ID and I'm just going to say one and we also have an error here and we can say that error is unused okay so now let's print out our user. ID and let's run this and see what happens our program here is going to panic the reason for that is because we've referenced a nil pointer here user is nil but we haven't checked to see if it is nil so we need to say if user is not equal to nil else now we can print this line down here so if you are returning pointers to signify that something is optional then make sure you're doing the check for nil otherwise it's probably usually a good idea to just return an empty version of the struct and you can see here if we return this empty version of the struct checking that whether this is n not is redundant now goes not going to let us do that and so we can just remove that entire block there now if we try to run this we're just going to get the default value for an INT which is zero so the last reason to use pointers is to save memory so let's say that we had this large struct here it's a struct and has lots of properties on it not just these two it has 100 other properties on it if we par this large struck into into a function we're going to create a new instance of that large struct so we're going to take up memory for double the size of this struct now instead what we might want to do is just pass in a reference so let's go back to our diagram and let's pretend in this block here we have our large struct and we also have a memory address and let's say this large struct here is 100 kilobytes so if we pass our large struct into another object we're going to take up another 100 kiloby of space inside of our memory but instead if we just pass in the address here we're only going to take up the amount of kilobytes that this or byes that this address takes up so these are the three main use cases for pointers if you enjoyed this video please make sure you leave a thumbs up and let me know what you want to learn about go next thank you for watching and I'll will see you in the next video
Info
Channel: TomDoesTech
Views: 1,777
Rating: undefined out of 5
Keywords:
Id: PR4BwsZaJY0
Channel Id: undefined
Length: 10min 5sec (605 seconds)
Published: Fri Feb 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.