Everything You Need To Know About Pointers In Golang

  Рет қаралды 27,923

Anthony GG

Anthony GG

Жыл бұрын

► Join my Discord community for free education 👉 / discord
► Become a Patreon for exclusive tutorials 👉 / anthonygg_
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
In this Golang tutorial, I will teach you everything you need to know about pointers in Golang. When to use them, and the most common pitfalls new developers will run into.
#golang

Пікірлер: 97
@anthonygg_
@anthonygg_ Жыл бұрын
► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz ► Become a Patreon for exclusive tutorials 👉 www.patreon.com/anthonygg_ To be precise. A pointer is not really an integer cause it cannot be negative. It is an "address" that points to a specific slot in memory. My excuses for using the word "integer" in this video. Thanks for watching
@TheDiveO
@TheDiveO Жыл бұрын
there are unsigned integers
@zoltanhorvath2238
@zoltanhorvath2238 2 ай бұрын
It actually shows how intelligent someone is, when they can explain something to you in a very simple way, that's what this guy does. Thanks Anthony!
@anthonygg_
@anthonygg_ 2 ай бұрын
Thank you very much!
@shamkirnet
@shamkirnet Жыл бұрын
thanks for the great video! a couple of points regarding pointers, perhaps it will be useful to someone. 1. Pointers allow you to bypass the copying of a variable, what allows you to save memory, as well as change the object by reference without copying it (this was already mentioned in the video) 2. The pointer value can be nil (var myPointer *int), so it is very convenient to use it, for example, where we return an object from the database, but under certain conditions the object may not be found in the database (the classic example is ORM): type UserID int type User struct { userId UserID firstName string lastName string } func FindUser(id UserID) *User { var userId UserId = 123 if id == userId { return &User{ userId: userId firstName: "First Name", lastName: "Last Name", } } return nil } func main() { if user := FindUser(UserID(123)); user != nil { fmt.Println(user.firstName + " " + user.lastName) } } 3. Sometimes it is convenient to create a pointer to a particular type with a default value that matches that type. To do this, you can use the "new" function, which returns a pointer to an initialized object in memory with a default value: { var myPointer *int // myPointer is nil } { myPointer := new(int) // myPointer is an address to an integer type with value 0 } You need to be careful by using pointers, because the uncontrolled use of pointers puts a load on the garbage collector.
@shamkirnet
@shamkirnet Жыл бұрын
one more thing regarding of pointers to reference types (map, chan, slice). it makes no sense to create pointers to them when passing them as an argument to a function in the hope of saving memory, since their structures already contain a pointer, so creating their copies does not lead to large memory costs.
@kevinfultz07
@kevinfultz07 Жыл бұрын
Wish I could hit the like button twice on this! Thanks man.
@anthonygg_
@anthonygg_ Жыл бұрын
Thank you!
@abdorootuae
@abdorootuae 8 ай бұрын
I will hit 3 time
@marckassel8593
@marckassel8593 3 ай бұрын
Man that’s a good video, hands on examples are always the key for me to understand something entirely. Thanks 🙏🏼
@tpotjj2979
@tpotjj2979 Жыл бұрын
Catching up on some of your videos today, great one again!
@felipechaves6100
@felipechaves6100 10 ай бұрын
Thanks a lot! This video did it for me! I understood pointer’s theoretically, but learning them in context is really helpful! It also helped me understanding methods in Go, they can be a bit confusing! Thanks 😊
@sovrinfo
@sovrinfo Жыл бұрын
Great video. Super big thanks!
@definitelynotrohan
@definitelynotrohan Жыл бұрын
thanks for such detailed explanation!
@someshmahajan8414
@someshmahajan8414 Жыл бұрын
Very informative video this helps me to understand the concept very clearly Thanks
@haoli5986
@haoli5986 5 ай бұрын
thank you so much! this is a very good explainer on a formally very confusing subject in Go for me
@harisudarsan211
@harisudarsan211 11 ай бұрын
Best youtube channel for golang till now awesome 🎉❤
@glengal6490
@glengal6490 Жыл бұрын
There is joy in just hearing him speak 🥰🥰
@jamshidbekyuldoshev7542
@jamshidbekyuldoshev7542 9 ай бұрын
Thanks I really like your explanation about pointers in GO
@asadsalehumar1011
@asadsalehumar1011 2 ай бұрын
amazing! really helpful👍
@web3Wizardss
@web3Wizardss Жыл бұрын
The Goat teacher 🤞
@nikta456
@nikta456 Жыл бұрын
Clean cut explanation
@denischiosa4496
@denischiosa4496 Жыл бұрын
good job man, very well explained
@anthonygg_
@anthonygg_ Жыл бұрын
Thanks!
@caderosche
@caderosche Жыл бұрын
Wow I've been looking for an explanation like this, being new to a language that heavily uses pointers.
@cooldudecs
@cooldudecs Жыл бұрын
pointers should not be used often unless absolutely necessary. Pass by value makes a lot of sense to limit accidental pointer mutation.
@MatheusCabraldosSantos
@MatheusCabraldosSantos Жыл бұрын
perfect! thank you.
@hamzadlm6625
@hamzadlm6625 Жыл бұрын
Subscribed instantly!
@anthonygg_
@anthonygg_ Жыл бұрын
🙏
@MaciejKoodziejczyk-jq9jo
@MaciejKoodziejczyk-jq9jo Жыл бұрын
awesome video
@___DS___
@___DS___ Жыл бұрын
Mate, thank you. I'm new to golang, coming from python. Even though the concept is clear itself with all them referencing to addresses in memory, the example with DB made the most sense to me lol. And offtop: I reckon u r Scottish, right? Ur accent made me writing this comment. I absolutely enjoyed it. You r trying so hard to not to swallow half of the letters and sounds they represent lol. So cute. I can't :)) Thank you. I had double pleasure listening to u. P.S. Don't consider it offensive or sth, I'm Ukrainian, so for me them things r nothing except amusing and endearing, representing your own culture, which I respect (and enjoyed).
@bashscript2805
@bashscript2805 Жыл бұрын
he is Italian
@_slier
@_slier Жыл бұрын
thank you GSP
@Nexjsdeveloper
@Nexjsdeveloper Жыл бұрын
Useful, undrestanble.
@TheJurabek
@TheJurabek Жыл бұрын
thanks mate, how you jump to previous line position after typing something in vim ? like you type “type” and jumped into previous line and typed “Player” and jumped again to previous line😊
@atlantic_love
@atlantic_love Жыл бұрын
Not saying that what you're saying isn't true, but I do find it humorous that on practically every tech-related video out there that host will say "a lot of people are commenting/asking" about (whatever issues is being talked about in video) as a sort of appeal to the masses :D
@anthonygg_
@anthonygg_ Жыл бұрын
True, I told myself to avoid that as much as possible. But you caught me here.
@pavelzapletal7942
@pavelzapletal7942 8 ай бұрын
Nice example, I am coming from TS world where you have no such pointers and you would just put result from function into a new variable. This is really nice feature in Go.
@cbaxtermusic
@cbaxtermusic 9 ай бұрын
Im curious what are your thoughts about adding nil checks at the beginning of functions that use pointers or pointer receivers? My pov is that it adds "necessary"(i say this loosely) code to your functions as its a time saver when avoiding with panics, although IMO the go complier panic does point you to where the nil deref did happen in the stack trace, so nil checks kind of are nice but not really necessary, just as you said, with great power comes great responsibility. im curious on your stance what i mean if im not clear is if fooStructPointer == nil {}
@anthonygg_
@anthonygg_ 9 ай бұрын
Well. It depends. Only if you know it might be nil, cause it can be nil
@ThomasSchlosser
@ThomasSchlosser Жыл бұрын
Thank you for the good, practical lesson about pointers! Two thoughts: 1) Your final recommendation was, only use pointers when you really need it. One reason you explicitly mentioned was „the object is very big“. But the more obvious one might be „some changes need to be made on the object“ - might be a good criteria for those who are unsure. 2) I am not sure, but I imagine that Golang has a „copy on write“ concept (a struct as a parameter is not copied physically unless it is changed - this could be decided roughly at compile time and exactly at runtime). If this would be the case, „chages need not be made“ would be the only case for using a reference (pointer). - I guess a little test programm like your example with the „bigdata“ func coud show, if memory cunsumption grows even if the func has no statement writing on the bigdata-struct. - Or do you already know if Golang has this sort of optimization?
@anthonygg_
@anthonygg_ Жыл бұрын
To be honest I don't really have a clue :). Like you said, maybe some material for exploration in code.
@renypacheco4812
@renypacheco4812 Жыл бұрын
Thanks for this detailed explanation, i just want to know what vscode theme are you using??
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@renypacheco4812
@renypacheco4812 Жыл бұрын
@@anthonygg_ thanks, hoping for more Go tutorials like this ❤️
@nikydobrev
@nikydobrev Жыл бұрын
@anthonygg_, can you tell me what VS Code theme you have been using?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@RichM1967
@RichM1967 Жыл бұрын
great video. What I found however is that I was creating a struct for the program's configuration, every function in the program used the struct, so every function had "func (conf *Conf) DoSomething()" -- Instead of doing that, is it bad to just declare conf as a global variable? Are global variables bad?
@anthonygg_
@anthonygg_ Жыл бұрын
Well depends, globals have there usecase sometimes
@luismarcilio
@luismarcilio Жыл бұрын
Hey @Antony if you see this comment: I have a doubt: when you pass a pointer, it’s passing the very pointer to the stack as in C language? Or a copy of the pointer that is pointing to the same memory area? Because if it’s the second option, this will require a garbage collection right?
@smithshelke2036
@smithshelke2036 11 ай бұрын
I think everything is a copy in golang. Same goes for passing pointers
@randomness3231
@randomness3231 Жыл бұрын
Hey @Anthony - do you have a video on how you use VS Code?
@anthonygg_
@anthonygg_ Жыл бұрын
Will make one tomorrow
@randomness3231
@randomness3231 Жыл бұрын
​@@anthonygg_ wow! thats awesome! I like the way you don't need a mouse. and how you move between tabs, terminal..
@nth-prog8562
@nth-prog8562 Жыл бұрын
Anthony, what theme do u use in vscode?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@nth-prog8562
@nth-prog8562 Жыл бұрын
@@anthonygg_ thanks
@DavidMwangikamau
@DavidMwangikamau 8 ай бұрын
@Jarek.
@Jarek. 5 ай бұрын
GoLang is really confusing in the way how it treats inside the function parameters passed by value - it's always a copy! But then if it's a struct - modifications inside the function won't be visible outside (as your local changes are lost). If it's a slice - you can modify existing elements but if slice capacity changes, it will be lost (as inside the function you have only a copy of slice signature with pointers to the data, length and capacity). Similarly in maps... To get it right, you need deep understanding how data around struct, map, slice is organised and what is actually passed by value.
@thiagodias-lo6wn
@thiagodias-lo6wn Жыл бұрын
Why Go/The IDE does not check the type of Player as nil before letting it execute and return the nil pointer error?
@anthonygg_
@anthonygg_ Жыл бұрын
Hmm good question will take a dive
@wuilliam321
@wuilliam321 Жыл бұрын
I use pointers when nil has a meaning other than the empty struct value. Not so often, but, is it an anti-pattern?
@anthonygg_
@anthonygg_ Жыл бұрын
I do the same, but its not always the best option. But its easy to compare against
@Simple_OG
@Simple_OG Ай бұрын
As a c/c++ its fell like home 😊
@jainilshah7907
@jainilshah7907 Жыл бұрын
What's the difference between func and methods ?
@anthonygg_
@anthonygg_ Жыл бұрын
Go has no methods. Function receivers are syntactic sugar
@k4rshh49
@k4rshh49 Жыл бұрын
Really don't like this guy's attitude but the content is gold - this was an awesome explanation haha 🤡
@anthonygg_
@anthonygg_ Жыл бұрын
The king does whatever the fuck he wants. But thanks ❤️
@tintin537
@tintin537 Жыл бұрын
The league of extra ordinary developers!
@anthonygg_
@anthonygg_ Жыл бұрын
Teemo top
@anthonygg_
@anthonygg_ Жыл бұрын
Or feed
@tintin537
@tintin537 Жыл бұрын
@@anthonygg_ 😂Fiora top or Jax
@anthonygg_
@anthonygg_ Жыл бұрын
@@tintin537 Be ready. Xmass holidays we gaming LIVE on stream. Do NOT let me down. Hence, carry me.
@tintin537
@tintin537 Жыл бұрын
@@anthonygg_ I play on eune servers what server do you play on?
@MatheusCamposdaSilva
@MatheusCamposdaSilva 9 ай бұрын
never thought I would be learning programming from Johnny Sins himself
@anthonygg_
@anthonygg_ 9 ай бұрын
I can learn you some other stuff also 😂
@MatheusCamposdaSilva
@MatheusCamposdaSilva 9 ай бұрын
@@anthonygg_ LOL, I'll pass this one... Awesome vid btw
@ruslangabitov5202
@ruslangabitov5202 Жыл бұрын
The only thing you should explain is how to DELETE object in Golang. 😅
@user-fu2kg4ug2q
@user-fu2kg4ug2q 7 ай бұрын
'Who's using a 32-bit machine in 2022' You got a lot of Arch users screaming slurs right now :D
@mahmoudabdelsattar8860
@mahmoudabdelsattar8860 Жыл бұрын
Can you make real project depends on pointers , or give us more pointers examples, specially with big.Int
@VyceC
@VyceC Жыл бұрын
15:57 it's wrong when you say "it's a function and not a method"
@devoverlord
@devoverlord Жыл бұрын
First
@anthonygg_
@anthonygg_ Жыл бұрын
You are even faster than my own pinned comment.
@demyk214
@demyk214 10 ай бұрын
Man golang intellisense is sloom op windows ik snap wel wrm jij ubuntu gebruik
@ayushsrivastava830
@ayushsrivastava830 11 ай бұрын
Bro is coding grenade logic for pubg 💀
@anthonygg_
@anthonygg_ 11 ай бұрын
LMAO
@AlokTripathi
@AlokTripathi Жыл бұрын
Hi Anthony, If my understanding is correct, here kzbin.info/www/bejne/o6KrY2SjZbKmjc0: ```func (player Player) takeDamageFromExplosion() { } ``` is a method ? If not, can you please explain why?
@imnishantsharma
@imnishantsharma 11 ай бұрын
I had same doubt but I think it's a method takeDamageFromExplosion has a receiver of type Player, indicated by (player Player) before the method name.
@AlokTripathi
@AlokTripathi 11 ай бұрын
@@imnishantsharma that's true! you are right.
@sirajul-anik
@sirajul-anik 9 ай бұрын
Your videos are good and very much informative. But i would request you to put some effort on typing. It is very much irritating to see all those mistakes throughout each and every videos. If you can't control those typing mistakes, keep it that way. Mechanical kb doesn’t help typing. Sorry if that hurts.
@anthonygg_
@anthonygg_ 9 ай бұрын
I feel ya. It is indeed very annoying. Even for me. It depends on the day though. I think it gets better in later videos.
@josundev
@josundev Жыл бұрын
I want to hit the like button, but it's on 666 :(
@anthonygg_
@anthonygg_ Жыл бұрын
🥲
@KimberlyWilliamsch
@KimberlyWilliamsch 3 ай бұрын
Pointers, learn C instead
How To Build And Structure A JSON API Project In Golang!?
23:23
Anthony GG
Рет қаралды 11 М.
How To Structure Your Golang (API) Projects!?
20:28
Anthony GG
Рет қаралды 43 М.
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 24 МЛН
DELETE TOXICITY = 5 LEGENDARY STARR DROPS!
02:20
Brawl Stars
Рет қаралды 15 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 27 МЛН
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Рет қаралды 66 М.
GoLang: 10+ UNIQUE Concepts/Conventions that Beginners Should Know About!
13:56
120x Faster Algorithm By Nested Loops
21:21
ThePrimeTime
Рет қаралды 333 М.
An Introduction to Targeting Web Assembly (WASM) with Golang
7:01
The Only Database Abstraction You Need | Prime Reacts
21:42
ThePrimeTime
Рет қаралды 186 М.
Advanced Golang: Generics Explained
13:37
Code With Ryan
Рет қаралды 55 М.
How To Build A Chat And Data Feed With WebSockets In Golang?
20:45
are "smart pointers" actually smart?
9:44
Low Level Learning
Рет қаралды 69 М.
Important Tips On How To Write Idiomatic Code In Golang
21:52
Anthony GG
Рет қаралды 20 М.
Concurrency in Go
18:40
Jake Wright
Рет қаралды 606 М.
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 24 МЛН