Everything You Need To Know About Pointers In Golang

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

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 5 ай бұрын
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_ 5 ай бұрын
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 🙏🏼
@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.
@glengal6490
@glengal6490 Жыл бұрын
There is joy in just hearing him speak 🥰🥰
@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 😊
@tpotjj2979
@tpotjj2979 Жыл бұрын
Catching up on some of your videos today, great one again!
@harisudarsan211
@harisudarsan211 Жыл бұрын
Best youtube channel for golang till now awesome 🎉❤
@web3Wizardss
@web3Wizardss Жыл бұрын
The Goat teacher 🤞
@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
@someshmahajan8414
@someshmahajan8414 Жыл бұрын
Very informative video this helps me to understand the concept very clearly Thanks
@sovrinfo
@sovrinfo Жыл бұрын
Great video. Super big thanks!
@nikta456
@nikta456 Жыл бұрын
Clean cut explanation
@jamshidbekyuldoshev7542
@jamshidbekyuldoshev7542 Жыл бұрын
Thanks I really like your explanation about pointers in GO
@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.
@_slier
@_slier Жыл бұрын
thank you GSP
@asadsalehumar1011
@asadsalehumar1011 6 ай бұрын
amazing! really helpful👍
@ASmith2024
@ASmith2024 Ай бұрын
Great video!
@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 Ай бұрын
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.
@MatheusCabraldosSantos
@MatheusCabraldosSantos Жыл бұрын
perfect! thank you.
@Nexjsdeveloper
@Nexjsdeveloper Жыл бұрын
Useful, undrestanble.
@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.
@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
@DavidMwangikamau
@DavidMwangikamau 11 ай бұрын
@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
@Simple_OG
@Simple_OG 4 ай бұрын
As a c/c++ its fell like home 😊
@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😊
@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?
@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..
@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 ❤️
@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
@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
@nth-prog8562
@nth-prog8562 Жыл бұрын
Anthony, what theme do u use in vscode?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@nth-prog8562
@nth-prog8562 Жыл бұрын
@@anthonygg_ thanks
@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 ❤️
@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
@ruslangabitov5202
@ruslangabitov5202 Жыл бұрын
The only thing you should explain is how to DELETE object in Golang. 😅
@nikydobrev
@nikydobrev Жыл бұрын
@anthonygg_, can you tell me what VS Code theme you have been using?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@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
@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
@Necr0e1
@Necr0e1 Ай бұрын
it boggles the mind when people try to explain concepts like pointers and others without giving a practical example like this
@VyceC
@VyceC Жыл бұрын
15:57 it's wrong when you say "it's a function and not a method"
@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.
@devoverlord
@devoverlord Жыл бұрын
First
@anthonygg_
@anthonygg_ Жыл бұрын
You are even faster than my own pinned comment.
@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
@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
How To Build And Structure A JSON API Project In Golang!?
23:23
Anthony GG
Рет қаралды 12 М.
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 61 М.
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3 МЛН
The CUTEST flower girl on YouTube (2019-2024)
00:10
Hungry FAM
Рет қаралды 55 МЛН
My daughter is creative when it comes to eating food #funny #comedy #cute #baby#smart girl
00:17
Spongebob ate Patrick 😱 #meme #spongebob #gmod
00:15
Mr. LoLo
Рет қаралды 10 МЛН
When You Should Actually Use Pointers In Go
16:34
Bryan English
Рет қаралды 15 М.
Golang Error Handling Is Better Than You Think!
18:53
Anthony GG
Рет қаралды 27 М.
Golang's Mocking Techniques - Kyle Yost | hatchpad Huddle
41:20
This is your last video about Golang Structs!
15:57
Flo Woelki
Рет қаралды 5 М.
A Practical Guide to Pointers in Go
10:05
TomDoesTech
Рет қаралды 2,9 М.
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Рет қаралды 464 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 109 М.
Important Tips On How To Write Idiomatic Code In Golang
21:52
Anthony GG
Рет қаралды 22 М.
I'm Coming Around To Go...
21:33
Theo - t3․gg
Рет қаралды 115 М.
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3 МЛН