Mastering And Taming NIL Pointers in Golang For Beginners

  Рет қаралды 8,301

Anthony GG

Anthony GG

Күн бұрын

Пікірлер: 35
@anthonygg_
@anthonygg_ 6 ай бұрын
► 33% OFF on my Go + HTMX + Templ Course PRESALE bit.ly/3UFruxO ► Join my Discord community for free education discord.com/invite/Ac7CWREe58 ► Exclusive Lessons, Mentorship, And Videos www.patreon.com/anthonygg_ ► 60% OFF on my Golang course fulltimegodev.com Thanks for watching
@destinlee
@destinlee 6 ай бұрын
hahaha. Rule of thumb: If you don't need a pointer don't use a f**king pointer!! Locked in. Love it
@dineshsilwal9687
@dineshsilwal9687 6 ай бұрын
Good stuff, Anthony. I was following the correct approach in my codebase. I see my collegues using pointer even in get apis which is no need and i have been correctly then frequently
@vitiok78
@vitiok78 6 ай бұрын
I even think that if your project doesn't take millions of requests a second then you can try to write only pure functions that don't mutate their arguments. That InsertUser function could return a fresh copy of the User with a different age
@KevinONeillPlus
@KevinONeillPlus 6 ай бұрын
I tend to use pointers for services and values for, ahem, values. One thing that caught me out in the early days was that if you call a function that returns an interface, and you happened to get a “nil” value back, you can check the value for nil and it’s all good, but try and access something on the value and boom, nil pointer error.
@vitiok78
@vitiok78 6 ай бұрын
Excellent point about using pointers only when it's really necessary. I used them everywhere trying to "save performance". It was so stupid... Even if it is a struct you won't have a huge performance difference between copying a pointer vs copying a struct.
@Laflamablanca969
@Laflamablanca969 6 ай бұрын
I guess the only counter argument I’d have to trying to avoid returning pointers, is I’d rather the error fail spectacularly than silently. It’s much easier to detect an issue if it’s loud as opposed to erroring silently and using the default value. I guess it really just comes down to experience when writing code that utilises pointers
@anthonygg_
@anthonygg_ 6 ай бұрын
True
@christ.4977
@christ.4977 6 ай бұрын
I'm not a programmer but I want to learn go. This is great content!
@vasanthkorada4802
@vasanthkorada4802 6 ай бұрын
Thanks. need more videos like these
@davidhinojosa3680
@davidhinojosa3680 6 ай бұрын
Good stuff, Anthony. We should really avoid using pointers in our own packages when starting, and only start adding them when we need some optimization
@krispekla
@krispekla 6 ай бұрын
Returning pointers isn't a bad thing if you know what you are doing. One case when I use them is in storage/db/repo layer when I query db and create new struct where I map result. It is important to understand what happens under the hood. If I would return just struct, go compiler would then copy this struct I created and create new one that it would return and then deallocate first one. Other way around with pointer, pointer would be created on heap, just passed back up and later be ready for GC. Now imagine that your structs are big this would mean it would take more from your processing power/compute.
@mihneabuzatu784
@mihneabuzatu784 6 ай бұрын
Returning a structure from a function is just a move operation and is very fast. This is because allocating and deallocating from the stack is very fast, and modern processors are very good at memory copying. You would need to have very large structs (200+ bytes) to maybe see a performance increase (considering heap allocation + cache miss). This is the kind of optimization which you should benchmark first.
@krispekla
@krispekla 6 ай бұрын
@@mihneabuzatu784 Interesting to know!
@jmatya
@jmatya 6 ай бұрын
Hey, good video, what I'd like to get some insights (old programmer new on golang) is when to embedded structs by refs in struct rather than struct by values
@lancemax857
@lancemax857 6 ай бұрын
One way I know when to use and not to use pointer is based on how I want a variable to be treated. If I want a variable to be treated like an immutable variable then I would not use pointer, if I want to treat a variable like a mutable object and allows functions to change the values then I would use pointers.
@beck4715
@beck4715 6 ай бұрын
I made a "HashPassword" function that returned a pointer to a string before I knew better 😂
@Dave.Wattz100
@Dave.Wattz100 6 ай бұрын
Sometimes I am fetching something from the database, but in case of no records, I don't want error to be raised, therefore it is better to define method in a way that it returns pointer to the (let's say) user. By doing so, I can further in the code check if user != nil if I need it. I think this is more convenient than defining a method in a way that it returns a value and then if user is not found, ignoring that specific error. I think so because with using pointers, code is more neat. Yes it produces a risk of getting a panic, but that's a skill issue if you ask me. Please correct me if I am speaking nonsense.
@roy_c
@roy_c 6 ай бұрын
You could create a sentinel error like ErrUserNotFound or similar and handle with errors.Is. I think is more expressive.
@MatheusSantos-gy5pi
@MatheusSantos-gy5pi 6 ай бұрын
In this case a return the struct and if the id is 0 i know that i didn't find any data. I don't know if it's correct, i started now to learn go
@EduarteBDO
@EduarteBDO 6 ай бұрын
@@MatheusSantos-gy5pi checking if id is 0 it's a pretty obscure way to handle it. Only you will know how to handle it, and you could forget about that. Or forget to handle the id == 0. Returning a error it's more expressive
@MatheusSantos-gy5pi
@MatheusSantos-gy5pi 6 ай бұрын
@@EduarteBDO So, how do i handle when it is a error from db and when is a error when i dont find a specific user for example?
@WiecejNoxiego
@WiecejNoxiego 6 ай бұрын
@@MatheusSantos-gy5pi You switch on the error type.
@serhat4571
@serhat4571 6 ай бұрын
What's your theme for vs code?
@anthonygg_
@anthonygg_ 6 ай бұрын
Gruvbox
@AminShahbaghi
@AminShahbaghi 5 ай бұрын
Thanks
@damien309
@damien309 6 ай бұрын
The whole pointer vs value debate is useless when it comes to performance. Don’t optimise in the dark or based on assumptions, the only right way to optimise is by profiling the app
@anthonygg_
@anthonygg_ 6 ай бұрын
Wise words.
@SR-ti6jj
@SR-ti6jj 6 ай бұрын
I use pointers by default, but I'm a bad man
@mansourbaitar5194
@mansourbaitar5194 6 ай бұрын
HIM
Why Golang's IO.READER Is More Important Than You Think
6:38
Anthony GG
Рет қаралды 14 М.
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Рет қаралды 81 М.
Миллионер | 2 - серия
16:04
Million Show
Рет қаралды 1,5 МЛН
怎么能插队呢!#火影忍者 #佐助 #家庭
00:12
火影忍者一家
Рет қаралды 16 МЛН
"كان عليّ أكل بقايا الطعام قبل هذا اليوم 🥹"
00:40
Holly Wolly Bow Arabic
Рет қаралды 7 МЛН
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34
This Will Make Everyone Understand Golang Interfaces
21:03
Anthony GG
Рет қаралды 53 М.
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 63 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 114 М.
A Practical Guide to Pointers in Go
10:05
TomDoesTech
Рет қаралды 3,4 М.
Just enough C to have fun
39:29
Kay Lack
Рет қаралды 54 М.
Become a bash scripting pro - full course
36:00
CODE IS EVERYTHING
Рет қаралды 61 М.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Anthony GG
Рет қаралды 24 М.
Pros vs Cons of When to USE Pointers in Golang
7:53
Melkey
Рет қаралды 11 М.
Structure Your Golang Service With Layers Like This
7:59
Anthony GG
Рет қаралды 14 М.
Миллионер | 2 - серия
16:04
Million Show
Рет қаралды 1,5 МЛН