Тёмный

Pros vs Cons of When to USE Pointers in Golang 

Melkey
Подписаться 26 тыс.
Просмотров 8 тыс.
50% 1

Go is an incredibly good programming language. It is gaining in popularity and is increasingly being used in fields such as cloud computing, backend and infrastructure.
In this video I go through a Reddit article that discusses how and when to user pointers in Go. This is a popular question so we go through it here!
Code: github.com/Melkeydev/go-bluep...
Video Editor: @TheMason
Twitch
I stream live on Twitch every weekend
Twitch : / melkey
Join the amazing community on Discord
Discord: / discord
I post memes and host Twitter Tech Spaces
Twitter: / melkeydev
Master the essentials of using and returning pointers in Go programming! This quick guide covers when and why to use pointers to enhance performance and manage memory efficiently. Perfect for beginners and intermediate Go developers, this video will clarify key concepts and best practices for using pointers effectively
Pros vs Cons of Returning Pointers In Go
Using Pointers Just for the Sake of Nil In Go
THIS Is When You Should Return Pointers in Golang
SUBSCRIBE OR GET LAID OFF
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #neovim #typescript #programming #vim #softwareengineering #codinglife #webdesign #webdevelopment #webdev #javascript #rustlang #rust #twitch #twitchstreamer #programmerhumor #codinghumor #software #softwareengineer #softwaredeveloper #softwaredevelopment #gymbro #gym #programmerhumor #programming #coding #golang #go #golanguage

Наука

Опубликовано:

 

8 май 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 41   
@MelkeyDev
@MelkeyDev 12 дней назад
I hope you all enjoy this video! Let me know what you think in the comments
@fersalamanca2606
@fersalamanca2606 9 дней назад
I think that for us Go noobs some code examples illustrating these points would be hugely benefitial
@casadogaspar
@casadogaspar 6 дней назад
True
@themichaelw
@themichaelw 8 дней назад
*context* I've been working full time in Go at an org with > 1000 engineers for the past 2 years, and I've read The Go Programming Language cover to cover. So I'm a decent gopher by most standards. I pretty much only _pass_ pointers when mutability of the original object is desirable. I _use_ pointers in structs when a value is optional, eg. when parsing from json. Objects like slices and maps are *no more efficient to pass to as a pointer than by value*, since the "value" of a slice/map is a "header" struct with a 3 fields denoting length, capacity, and a pointer to the first address of the slice. So there's already a pointer under the hood, no need to create another. Also, pass-by-value is typically faster for small things. And if you're running on 64 bit hardware and you're passing pointers to < 64 bit variables like int32, you're literally using twice as much space to pass that as a pointer than you would to just copy the 32 bit value, since a pointer will be full word, or 64 bits on that architecture.
@rosehogenson1398
@rosehogenson1398 12 дней назад
Whenever you return an invalid value from a function, either nil or an uninitialized struct, it's good to include an extra bool (or error) return as well. For example: func f() (*MyStruct, bool) { if !somePrecondition() { return nil, false } return &MyStruct{}, true } This greatly reduces the chance that the caller will forget to check for nil, and get a nil pointer dereference. This comes from the builtin map type, and the _, ok := pattern.
@MelkeyDev
@MelkeyDev 12 дней назад
OOOO This is awesome. And if you want to return an error, would you just append it?
@johanneswelsch
@johanneswelsch 10 дней назад
thanks, makes sense
@cristophermoreno2290
@cristophermoreno2290 12 дней назад
🔥I use pointers to: 👉 `mutate` data between 2 isolated `code blocks (functions)` 👉 Avoid new memory allocations 👉 make the Garbage Collector happy ... Thank you for the great video !
@miniappletheapple
@miniappletheapple 12 дней назад
Actually heap pointer is bad for GC
@MelkeyDev
@MelkeyDev 12 дней назад
Nice work!
@tamdomus
@tamdomus 12 дней назад
2 More pros: - Can use methods with both pointer and value receivers. - Does not copy the sync.Mutex
@MelkeyDev
@MelkeyDev 12 дней назад
Aye awesome thank you
@justintie
@justintie 12 дней назад
Copying a struct is much faster than messing with pointers, unless it is a huge struct
@MelkeyDev
@MelkeyDev 12 дней назад
Yep. If its a huge struct then copying becomes inefficient
@TehKarmalizer
@TehKarmalizer 10 дней назад
This is why I think mixed API isn’t a strong con. It may be warranted for performance, and doesn’t strike me as very different from simply returning different types.
@afsdab
@afsdab 12 дней назад
I'm learning Go and for now I'm returning in every function I have, but I watched a bunch of videos that recommend if I'm getting info and I won't do any change in the state the function should not return a pointer. I'll give a try for this approach and see what happens
@MelkeyDev
@MelkeyDev 12 дней назад
I think thats a good rule of thumb. In all honestly, on smaller scale apps, either approach will be good
@jordangregory9852
@jordangregory9852 12 дней назад
Great video! Personally I’m purely in the “pointer for mutability only” camp. I default to copies so I can’t mutate things on accident.
@MelkeyDev
@MelkeyDev 12 дней назад
Hell yeah. Love it
@amadijerry7607
@amadijerry7607 9 дней назад
I started learning GO, a few months ago. How we can structure our codes to prevent import cycle errors
@MattRobinsonDev
@MattRobinsonDev 6 дней назад
Great video
@erikslorenz
@erikslorenz 12 дней назад
I tend not to use pointers when it’s record type values. For example user. A lot of the times it’s coming out of a context and I need that to be a real val
@johanneswelsch
@johanneswelsch 10 дней назад
What do you think about filling up DB with values instead of nil? I've been bitten by this before where a nil db field could not be scanned into a struct field. It was a runtime error.
@LuisPinto
@LuisPinto 12 дней назад
Hey @MelkeyDev do you have any suggestion for a book/course/any other resource to learn computer science concepts in Go?
@MelkeyDev
@MelkeyDev 12 дней назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-qT14b1pxtiI.html&t I made this video - let me know if its helpfu
@paca3107
@paca3107 11 дней назад
Please make a video how to avoid memory leaks!
@viniciusmachadorodrigues1724
@viniciusmachadorodrigues1724 10 дней назад
I only use pointers when i purposelly want to modify the struct somewhere.
@emil_l889
@emil_l889 12 дней назад
Once spent like an hour getting nil pointer deference when working w redis only to realize that I forgot to initialize the struct😭😔
@MelkeyDev
@MelkeyDev 12 дней назад
Classic.
@kenzo3477
@kenzo3477 12 дней назад
use pointers in C 🚫 use pointers in go ✅
@peerlesstuber9693
@peerlesstuber9693 12 дней назад
I just recently ran into this issue being new at go. I needed to be able to handle three situations for my database api: (1) searching for NULL values, (2) searching for present values like "hello", (3) skipping the parameter altogether so as to not make it a constraint. The best way I found to do that so far is by making a "nullable argument" where nil means to search for NULL and an empty string means to skip. Please correct me if you have a better solution!!!
@ofadiman
@ofadiman 12 дней назад
Could you point me to the nearest grocery store using golang?
@MelkeyDev
@MelkeyDev 12 дней назад
yea
@AntonLukas
@AntonLukas 12 дней назад
Most of the downsides appear to just be skill issues
@MelkeyDev
@MelkeyDev 12 дней назад
Everything is a skill issue if you break it down
@AntonLukas
@AntonLukas 11 дней назад
@@MelkeyDev Fair enough, great video by the way.
@luizpbraga
@luizpbraga 12 дней назад
return a stack pointer is just weird
@MelkeyDev
@MelkeyDev 12 дней назад
its all weird
@erikslorenz
@erikslorenz 12 дней назад
Nah always give the shit struct back and an error
@sirbuttonhd
@sirbuttonhd 12 дней назад
first first first
@MelkeyDev
@MelkeyDev 12 дней назад
HELL YEAH
Далее
Go Pointers: When & How To Use Them Efficiently
14:09
Why I use THIS Golang RPC Framework
13:46
Просмотров 4,1 тыс.
THIS is the BEST Way to Write HTTP Services in Golang
13:53
Pretty much every website uses the wrong font size…
15:33
Why You Should Learn Go
17:35
Просмотров 11 тыс.
i changed my mind about zig
9:34
Просмотров 131 тыс.
Is 2024 The Year Of Zig ?
48:20
Просмотров 88 тыс.
Golang and the Billion Dollar Mistake
11:03
Просмотров 7 тыс.
Why You Should AVOID Linked Lists
14:12
Просмотров 265 тыс.
Life After SQL (EdgeDB Is Fascinating)
15:40
Просмотров 49 тыс.
Нужно ли чистить ПК от пыли?
0:59
Phone charger explosion
0:43
Просмотров 41 млн