Mastering And Taming NIL Pointers in Golang For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for a lot of people especially beginnner programmers coming from other languages that do not have this low-level experience the the Neil pointed the reference error or Panic is still quite overwhelming and very confusing so I want to make this small little video where we go into a little bit in depth especially for these beginners uh programmers uh coming to goang to understand exactly why this is happening and how we can basically eliminate this error as much as possible guys before we continue 60% of my viewers are not yet subscribed to my channel you could do me and my grandma an enormous favor to subscribe to my channel so we can push this content to way more people here on YouTube because I promise Grandma to have 100K subscribers by the end of the year and for the people that really want to level up as a go engineer I have these beautiful two courses here all the links are down in the description and uh very important is the HDMX goang Temple course still has for one week 33% off it's completely finished here uh this is the curriculum you can pause the video or you can basically slow the video down if you want to see what we're what we are building we're building a complete AI application uh from scratch uh from setting up the project structure to basically uh continuously deployment on digital ocean and some better stack logging so check that out if that interests you all the links are down in the description let's go back into the origins of the pointer and what's a no pointer and what's a d reference right so basically let's say we're going to have a type of a user classic the classic example it's going to be a structure and let's give this an email uh which is going to be a string and also give this an H which going to be an integer right so in Goan we can do something like User it's going to be a user here initialize this thing and we can say email uh a g.com for example and then we're going to say the H let me delete that uh the H is going to I don't know 20 right and then we can say f&t parental and users very simple let's clear go and may not go boom just like that and we see a.com and the AG is 20 very simple what we also can do is we can delete these things right and uh what's going to happen is that goang will basically let me show you so let's do this again boom golang is going to initialize this structure initialize it member fields to the default value of that type for a string it's going to be an empty for an integer it's going to be zero for a float it's always going to be zero and all that stuff right very simple very cool concept right so this is fine this is basically initial an initialization of a user structure we can also do this right ver user is going to be a user which is actually not an initialization this is a declaration but still this going to be working just fine which is amazing right we can also basically access this H for example uh which is going going to be zero because we didn't initialize it but kolang will initialize this to the default value of an endt which is zero just like that perfectly fine all good everything is at equilibrium but the problem starts when we are going to say that this user here is going to be a pointer so if we delete this AG here and you can already see it basically here look at that vs code is actually warning you that this is a new reference you know it's actually pretty cool uh but n nonetheless about that that uh if you use something else that cannot detect that you basically screwed but hey so this is basically a pointer declaration no it's not an initialization so if we do this did I save that yeah so if we do this again you're going to see it's going to be nil perfectly fine but the problem starts that if we actually going to call a member field on this mil pointer right it's going to cause this runtime error invalid memory address or pointed the reference and this Panic can especially for for I know a lot of you are already experienced and you understand this but there are a lot of people coming from other languages that this is kind of very impress very overwhelming very confusing um what is just happened right what just happened so basically we need to go back to C so a lot of you probably have already C or C++ experience maybe uh a lot of you have n I'm going to explain so let's say we're going to have this user here in C and we call user. email right this is basically just calling a member uh value on that structure but if this is a pointer in C or if this is a pointer in C++ what you need to do is you need to call this with this Arrow I think it's the arrow syntax I don't know correct me if I'm wrong or how this is called the arrow syntax whatever and what this means is you're telling the compiler that listen compiler I want to dreference my user pointer and grab the email value right so how does this actually work so you're going to have a memory right memory rum on your PC so this is basically memory right now I'm going to explain it in a very simple way right this is the power of my channel explaining difficult Concepts in a very simple way if you really want to know these in depth you need to do your own due diligence right first you need to know the higher level simple concept before you can go deeper you know what I mean uh same with a woman right so what we going to do is uh we're going to have our structure here right it's going to be the double click the user struct right and let's say this is a pointer to be honest uh to make sure let's say it's going to be pointer n x blah blah blah blah something like that the pointer okay so if this is a pointer and you're going to access email for example it's going to if you use the arrow syntax in C it's going to dreference this user structure which basically means like hey let me actually make this even better user email something like that it's going to dreference this pointer right this this thing points to somewhere in memory it's going to to that memory and it's going to grab this email right it's going to find this email somewhere in memory and it's going to return that value it's that simple that's what happening in goang in goang we don't have the syntax like that it's a very simple language goang is just like the same way if it's a pointer or it's not a pointer we can access this email with a DOT notation right goang will basically check this type here right this user and you can see it's appointed to a user the compiler knows hey this is a pointer let's deference this if it's a pointer I'm going to deference this automatically without any other special syntax right that's the difference between C C++ and goang goang automatically detects if it's a poter or not and will D reference your pointer what's the reference well it's this right so the problem starts with if this pointer here right this user is nil what does that mean this is a declaration it points to nothing it's a n pointer what's a n pointer a n pointer is this points to nothing it will Point that's not 100% correctly true because it can point to Something in memory where the garbage lives right and you can see that in CN C++ um you going to have a pointer and it's going to point to something but it's not going to point to the thing you actually wanted to point at but that's something completely different let's not not go into this rabbit hole so it points to nil it points to nothing actually we need to be very careful of Reddit will come and Pitchfork it points to nothing nothing you know what I mean no not the NADA right so if we want to have this email here you know what I mean user. email but it points to nothing you're going to have this problem the nil pointer the reference because this pointer is nil we cannot reference it that's the thing that's what's happening but okay cool now we know that so how can we prevent that the rule of thump is if you do not need a pointer don't use a pointer but I because I see people doing this right for example a very simple common example is fetching users from a database or something uh so what's going to happen is and it does not need to be from a database it can be from something else I just giving you an example here right so for example you're going to have uh get user by ID right and we going to have an ID is going to be an integer and what does this return I see a lot of people I see a lot of code bases where they going to return pointers to this user and an error right uh and in this case uh let's do return just to make the compiler satisfied user and N right why are we returning a pointer here there is no need to ever ever return a pointer to the user ever right of course you could say yeah but Andy there is an error here so we always check the error so we never going to have this m user I completely agree but it's just an example on if you're going to start throwing pointers turning pointers and using pointers in your application like that for no reason at all you high there is a very high likely chance that you're going to somewhere sometime going to cause this this this midpoint of the reference right you know what I mean very simplified example I know but I'm a simple man right so this should actually be just like that right just like this thing because if you start using it like this where you just have no pointed values no pointer structures even though there is a problem where you could not fetch the user or there is something wrong and you didn't initialize it or whatever or you put it to this this you reset it whatever is happening you're never going to have this uh Panic it's going to use the default values the next step is basically okay where is well a good place what is well a good place to use a point for example if we have something like um insert user right we're going to insert a user and we're going to return here an errors right just like that and this user we're going to insert a user we're going to have this pointer to a user why do we want that well because the pointer is coming from outside of our scope so this if you use an insert user on your DB um package or your DB client what these clients are going to do what these packages are going to do they're basically going to insert your user and they're going to update the ID of your user right uh so how does it work let me actually make this ID here is going to be an integer right insert user let's say something like uh user ID is going to be one and then return n because it was no error right so and how do we use that well we're going to say user is going to be just a simple user no pointer no nothing Let's do an age here of uh 20 and then an email maybe uh something like that right no IDE because we don't know it and now we're going to say if eror is going to be insert user this user but we need to pause a pointer so we're going to take this reference here and then we going to say if the error is not n we are going to return n actually that's not true what am I doing here uh log fatal still sleeping error here boom uh FM parental and skill issues this us it right just like that we're going to see that the user is basically updated right the ID is updated by this DB so to say DB package right and this is a perfect so we don't use a pointer here we just take the reference here which will make this a pointer update the idea and here it's fine if we don't use this right if we don't use this pointer uh just like this and we're going to rerun it then you're going to see it's going to be zero right it cannot update it because it's no a pointer right anyway so these are some very simple tips and we can go into in depth the problem is attention span of the average user is going to be a couple of minutes and we're already at 12 minutes so that's already a problem right but these are very simple explanations that I'm doing here and to basically trigger your con your logical connections into this point of stuff to give you some more feedback some more context on how this is happening and how we can avoid that can we completely avoid pointers can we completely avoid new pointer the reference of course not but uh always check them try to avoid pointers if you do not use them especially with this get user by ID and all the other family of these kind of functions that will return a structure you don't need do do not need to return a pointer at all times because creating pointers will create a lot of garbage on the Heap and everything on the Heap every garbage that being created also needs to be uh garbage collected right so it's not always a speed benefit by using pointers I hope you had something about this video let me know in the comments what you think about that if I say something wrong correct me uh and I'm looking forward to see you in the next video or live stream peace out
Info
Channel: Anthony GG
Views: 7,468
Rating: undefined out of 5
Keywords: go lang, go language, programming, golang pointers, golang nil, go nil pointer, golang tutorial for beginners, golang, golang programming, go programming language, learn golang, go tutorial, golang for beginners, software development, go programming, computer science, programming tutorial, go tutorial for beginners, webdev, golang structs, software engineering, golang course, what is golang, software developer, golang tutorials, learn go programming online, web development
Id: MJfcZjbXGQA
Channel Id: undefined
Length: 13min 23sec (803 seconds)
Published: Tue Apr 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.