When You Should Actually Use Pointers In Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to my new office yeah i just moved so now my office is in the basement um get used to this view just very luxurious anyway we're going to talk about pointers in go today so if you come from the world of c and c plus plus you probably use pointers all the time which makes a lot of sense if you come from the world of java you never use pointers right so uh i'm here to tell you that pointers are not what you think uh completely from the cnc plus plus world so go is a garbage collected language right so that's one of kind of the sort of pillars of go it's garbage collected very small fast compiled binaries and it's native concurrency but the fact that it's garbage collected means that some special things are going on so believe it or not it's actually more costly to use pointers and go than it is to pass by value or return copies of whatever right and again because that's the nature of the garbage collected language so it has a stack like java does and so when we kind of go to that closing bracket everything betwixt is gone forever right and so if you do use a pointers go has to do some background work where it has to pluck things off of the stack and keep special track of them and that actually can slow us down significantly so today i'm going to talk about when you should actually use pointers in go and there's five situations i would say four plus a special case that you'll never see in the real world for the most part but uh yeah let's talk about when you should actually use one so the first case is using a pointer receiver so if you want to make a new thing and set that specific thing not a copy of it to a value then you would use a pointer receiver and this is kind of how go gets around the whole not really being an object-oriented language only having structures not classes right because if you look at classes in java or c plus you know they not only have methods but they also have values right and in go we only have structs so we only typically do values and so the way that you associate a function with a specific type is by using receivers right so a pointer receiver is you're modifying that original thing so when we use new right we're creating a pointer to a thing right and then here we say thing dot set id five so we're passing it here so what we would do is we would do t dot id is equal to new id right and so we would do within dot set id equal to four all right so print line dot id cool then we can also see here we'll do it inside this function as well t dot id all right so they'll do a go run so here you'll see it says four and then five right so we set it set it equal to four and then we set it and we set it again equal to uh five uh let's see if we take away what happens when we take away that pointer receiver though four zero and then five zero right so now you can see kind of see the difference in these pointers because here we didn't set this equal you know we we set it but when you don't use the pointer receiver it just creates a copy essentially um so that's one case where you would use pointers and that would be pointer receivers i would actually argue that if you're going to be doing this kind of thing don't um don't do it so i would if anything return a copy of this thing uh to a as just a return value right so instead of doing this where you would have like a you know a pointer receiver this is kind of a unique situation so go allows you to instantiate new variables in the function signature which is kind of cool so here i can say t t thang right and so that's telling this that i am going to i have a return type right so then i would say um you know um we would return t works because we got rid of the error so here uh for thing.setid right we would actually do this we would say thing one and then let's set this to 7 and we will do thing 2. all right this kind of achieves the the same effect here right because you can kind of see the difference so instead of doing a make or whatever right now here it's a little more concise and we've established what type we're going to be returning um to me this is a little bit cleaner and it does create a new thing so this isn't exactly what i wanted to focus on with this but really only use point to receivers i mean if you have absolutely no choice something i don't know maybe you have to lock some kind of um like you have a sync mutex that's a part of a structure and you need to modify in some way yeah okay then you're gonna have to do that right but really only do point to receivers if there can be one and only one ever and that's the golden rule thing that you have to reference other than that eh uh so uh another time that you should use uh pointers is let's say that you're passing by uh by reference right so then we would say um you uh pointer id star int you're only ever going to do that if there has there can only be one again using pointers is very expensive and go only use them if you have no choice you have to reference that single thing the next time that you would genuinely want to use pointers and go and here is one where it's actually a performance benefit i think this is the only one where it's genuinely a performance benefit and that is if you want to omit sub structures of json if it's empty right so here we have a thing struct right it has a pointer to a sub thing right and that sub thing is is this you notice the omit empties right so let's say that i run this here right so here we have two different things going on so if you look at line 21 right both of these are essentially the same but the substruct is is empty here so it still accounts for it it'll still print it out and say hey i am empty but what's going on underneath the hood is actually really really interesting so because technically the pointer to the sub thing is nil there's nothing there go will not instantiate the memory for it so you can get let's see i mean you had thousands of substructures i've seen it you can omit those and you can actually get a significant performance boost by by doing that i'll post a really interesting article that i found on this down in the descriptions but yeah having these pointers to substructures and omitting them if they're empty you can actually save a significant amount of memory depending on the kind of the massiveness of the the structure that you're going to be using so that's another case and probably the only case where using pointers and go is actually a really good performance boost so the last real world example that you're going to see the sort of the use of pointers is with uh optional values right so we talked a little bit about optional json structures but optional values are something that's really really interesting so let's say that you have a pointer to a bool right so here i'm going to say um bool 1 is equal to bool true right so let's also say that i create a pointer for it right so bool1 ptr is equal to a pointer to a bool one all right so let's uh print both of these out one print line one pointer now we run both of those now we create both of these pointers so if you do a go run right so we get true and we get the address right um let's say though that i come in here and i want bool 2 to equal a new bool that's false that's empty that's something we can do right so let's say i come in here and i print fmt now remember i haven't passed this a value it's just a bool that i've allocated right i am going to say google 2 so the memory address is there right what happens if i try to dereference it it'll automatically go to false interesting right interesting now let's get a little even a little crazier right so what if we set bool 2 which remember is set to bool is equal to nil right because guess what bull two is it's a pointer it's a pointer i can set pointers to nil now what happens if i try to print it nil so now all of a sudden we can have a third value how cool is that right so now bools can have a true a false and a nil right you can have an empty pointer or a non-empty pointer that's another situation where again it's not going to be a performance boost but it gives you more context as to what's going on this is probably one of the most useful things you will learn dealing with go i mean it's it blew my mind the first time that i learned about it and that's when i realized oh this is i can use this thing to create a new state uh associated with all with all of these things uh with all of these sort of primitive variables right that's really really interesting and that's actually very very valuable so the last one that i'm going to talk about is uh recursive data structures right and the reason that i will tell you that you really never see this in the real world is because you'll you'll see this pretty much exclusively with something like lead code so go actually provides you a linked list library or package whatever you want to call it uh that you can use kind of at your well and it provides you all those nice you know inserts and deletes and it's really really great right but if you do if you if you've done a coding interview in the last oh gee i don't know bazillion years typically you have to do some kind of asinine leap code question where it's like you know traverse a binary tree i don't know i don't like lead code at all it's not something i am particularly interested in doing um i'm glad there are people who are interested in that kind of thing because they're probably way better at algorithms and all this stuff better than i am i can create a binary tree i can convert a binary tree i can do singly linked lists doubling link i can make that stuff right so that's really the only other time that you're actually going to see pointers with go um i would argue that it's a very impractical example but you know whatever so type node struct right um you know id and phrase string and then let's say that it's a a doubly linked list of some kind so you would have a forward which would be a pointer to a node you'd have a backward it's also a pointer to a node so those are kind of the the four cases where you're going to see pointers used practically and then kind of like one impractical one so to go over it again you're doing pointer receivers which means that you have one thing that needs to be modified exactly you're doing pass by uh reference right so you have some kind of variable somewhere else and you can only modify that exact one then finally like a really really useful one which is if you want to emit substructures within json if you don't want the memory allocated for them if they are empty use a pointer and then easily one of the most useful things is being able to create a third state of concrete values like integers you now have three states for bools that's something really really cool and then of course you get this not practical but still useful data structure of a recursive data structure excuse me and that will be helpful for your coding interviews remember go is really really good the syntax for dealing with pointers is very very easy and i would argue that if you're going to have to be doing those silly silly interviews then this is a really good language to do it in so that's it that's my video thank you for watching if you have any questions please post them in the comments and i really appreciate your time this is really interesting stuff and i hope you're having a good day i hope you're surviving kovid you know yeah see ya
Info
Channel: Bryan English
Views: 4,000
Rating: undefined out of 5
Keywords:
Id: feinHNLSHWI
Channel Id: undefined
Length: 16min 33sec (993 seconds)
Published: Tue Sep 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.