Everything You Need To Know About Pointers In Golang

  Рет қаралды 29,645

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

Пікірлер: 101
@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 6 ай бұрын
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_ 6 ай бұрын
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 11 ай бұрын
I will hit 3 time
@paulezekiel-hart733
@paulezekiel-hart733 3 ай бұрын
i may be obsessed with your videos at this point, thanks again for a wonderful content
@ireallylikecoffee14
@ireallylikecoffee14 6 ай бұрын
Man that’s a good video, hands on examples are always the key for me to understand something entirely. Thanks 🙏🏼
@glengal6490
@glengal6490 Жыл бұрын
There is joy in just hearing him speak 🥰🥰
@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.
@tpotjj2979
@tpotjj2979 Жыл бұрын
Catching up on some of your videos today, great one again!
@web3Wizardss
@web3Wizardss Жыл бұрын
The Goat teacher 🤞
@harisudarsan211
@harisudarsan211 Жыл бұрын
Best youtube channel for golang till now awesome 🎉❤
@haoli5986
@haoli5986 8 ай бұрын
thank you so much! this is a very good explainer on a formally very confusing subject in Go for me
@definitelynotrohan
@definitelynotrohan Жыл бұрын
thanks for such detailed explanation!
@___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
@nikta456
@nikta456 Жыл бұрын
Clean cut explanation
@someshmahajan8414
@someshmahajan8414 Жыл бұрын
Very informative video this helps me to understand the concept very clearly Thanks
@jamshidbekyuldoshev7542
@jamshidbekyuldoshev7542 Жыл бұрын
Thanks I really like your explanation about pointers in GO
@sovrinfo
@sovrinfo Жыл бұрын
Great video. Super big thanks!
@ASmith2024
@ASmith2024 Ай бұрын
Great video!
@asadsalehumar1011
@asadsalehumar1011 6 ай бұрын
amazing! really helpful👍
@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.
@denischiosa4496
@denischiosa4496 Жыл бұрын
good job man, very well explained
@anthonygg_
@anthonygg_ Жыл бұрын
Thanks!
@hamzadlm6625
@hamzadlm6625 Жыл бұрын
Subscribed instantly!
@anthonygg_
@anthonygg_ Жыл бұрын
🙏
@NihadBadalov
@NihadBadalov 2 ай бұрын
Great video
@pavelzapletal7942
@pavelzapletal7942 11 ай бұрын
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.
@Nexjsdeveloper
@Nexjsdeveloper Жыл бұрын
Useful, undrestanble.
@MatheusCabraldosSantos
@MatheusCabraldosSantos Жыл бұрын
perfect! thank you.
@Jarek.
@Jarek. 8 ай бұрын
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.
@MatheusCamposdaSilva
@MatheusCamposdaSilva Жыл бұрын
never thought I would be learning programming from Johnny Sins himself
@anthonygg_
@anthonygg_ Жыл бұрын
I can learn you some other stuff also 😂
@MatheusCamposdaSilva
@MatheusCamposdaSilva Жыл бұрын
@@anthonygg_ LOL, I'll pass this one... Awesome vid btw
@EdwardWork-m5t
@EdwardWork-m5t 10 ай бұрын
'Who's using a 32-bit machine in 2022' You got a lot of Arch users screaming slurs right now :D
@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😊
@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 ❤️
@Simple_OG
@Simple_OG 4 ай бұрын
As a c/c++ its fell like home 😊
@ruslangabitov5202
@ruslangabitov5202 Жыл бұрын
The only thing you should explain is how to DELETE object in Golang. 😅
@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?
@felipechaves6100
@felipechaves6100 Жыл бұрын
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 😊
@DavidMwangikamau
@DavidMwangikamau 11 ай бұрын
@VyceC
@VyceC Жыл бұрын
15:57 it's wrong when you say "it's a function and not a method"
@Necr0e1
@Necr0e1 Ай бұрын
it boggles the mind when people try to explain concepts like pointers and others without giving a practical example like this
@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
@cbaxtermusic
@cbaxtermusic Жыл бұрын
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_ Жыл бұрын
Well. It depends. Only if you know it might be nil, cause it can be nil
@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
@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 Жыл бұрын
I think everything is a copy in golang. Same goes for passing pointers
@jainilshah7907
@jainilshah7907 Жыл бұрын
What's the difference between func and methods ?
@anthonygg_
@anthonygg_ Жыл бұрын
Go has no methods. Function receivers are syntactic sugar
@sirajul-anik
@sirajul-anik Жыл бұрын
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_ Жыл бұрын
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.
@mahmoudabdelsattar8860
@mahmoudabdelsattar8860 Жыл бұрын
Can you make real project depends on pointers , or give us more pointers examples, specially with big.Int
@demyk214
@demyk214 Жыл бұрын
Man golang intellisense is sloom op windows ik snap wel wrm jij ubuntu gebruik
@devoverlord
@devoverlord Жыл бұрын
First
@anthonygg_
@anthonygg_ Жыл бұрын
You are even faster than my own pinned comment.
@ayushsrivastava830
@ayushsrivastava830 Жыл бұрын
Bro is coding grenade logic for pubg 💀
@anthonygg_
@anthonygg_ Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@imnishantsharma that's true! you are right.
@josundev
@josundev Жыл бұрын
I want to hit the like button, but it's on 666 :(
@anthonygg_
@anthonygg_ Жыл бұрын
🥲
@KimberlyWilliamsch
@KimberlyWilliamsch 6 ай бұрын
Pointers, learn C instead
@MaciejKoodziejczyk-jq9jo
@MaciejKoodziejczyk-jq9jo Жыл бұрын
awesome video
@_slier
@_slier Жыл бұрын
thank you GSP
@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
@nth-prog8562
@nth-prog8562 Жыл бұрын
Anthony, what theme do u use in vscode?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@nth-prog8562
@nth-prog8562 Жыл бұрын
@@anthonygg_ thanks
@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
@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..
@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.
How To Build And Structure A JSON API Project In Golang!?
23:23
Anthony GG
Рет қаралды 12 М.
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Рет қаралды 78 М.
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 238 МЛН
Как подписать? 😂 #shorts
00:10
Денис Кукояка
Рет қаралды 7 МЛН
when you have plan B 😂
00:11
Andrey Grechka
Рет қаралды 66 МЛН
Learning Golang Context!! Never Looked At It!
25:03
TheVimeagen
Рет қаралды 36 М.
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 61 М.
Decrusting the tokio crate
3:31:48
Jon Gjengset
Рет қаралды 98 М.
Important Tips On How To Write Idiomatic Code In Golang
21:52
Anthony GG
Рет қаралды 22 М.
How You Should Use Mutexes And Atomic Values In Golang?!
15:49
Anthony GG
Рет қаралды 14 М.
Hacking with Andrew and Brad: tip.golang.org
1:02:49
The Go Programming Language
Рет қаралды 88 М.
Mastering And Taming NIL Pointers in Golang For Beginners
13:24
Compiler Q&A, September 2024
2:10:03
Jonathan Blow
Рет қаралды 22 М.
Creating custom struct tags in Golang is awesome!
24:42
Flo Woelki
Рет қаралды 7 М.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Anthony GG
Рет қаралды 23 М.