Тёмный

Defer Functions In Golang: Everything You Need To Know 

Flo Woelki
Подписаться 11 тыс.
Просмотров 1,2 тыс.
50% 1

Наука

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

 

6 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 13   
@dawnrazor
@dawnrazor Месяц назад
Good video, but there is an essential detail about the defer statement that you omitted which can be a really awkward gotchya if you’re not aware of it. And that is a defer creates a closure over local state. Your defer statement must make sure that any state that it interacts with is fully present at the time of the defer statement. Eg, let’s say the first thing inside of a func is a var declaration of an uninitiatialised variable. Next there is a defer that interacts somehow with this state. After the defer there is some logic that updates this state. If you think that when the defer actually runs it will see the updated version of that state, you will be surprised to learn that the version of that state the defer sees is the uninitialised version. This was a shock to me when I first discovered it and took me quite a while to realise what was happening. This occurs because the defer is working on the closure version of the state and hence only sees the uninitialised version. Hope this helps anyone to not fall into this subtle trap.
@rumenneshev9433
@rumenneshev9433 Месяц назад
This not always true, it depends on the defer declaration Test it with: func work() { var v string defer func() { fmt.Println("V in func's defer", v) }() v = "asd" fmt.Println("V in func", v) } Outputs: V in func: asd V in func's defer: asd BUT: func work() { var v string defer fmt.Println("V in func's defer", v) v = "asd" fmt.Println("V in func", v) } Will indeed output: V in func: asd V in func's defer:
@dawnrazor
@dawnrazor Месяц назад
@@rumenneshev9433 ok I’m not going to dispute your case, but my point still stands. Beware of how you do defer
@rumenneshev9433
@rumenneshev9433 Месяц назад
@@dawnrazor Yes, not meant to argue, just to note that there's a way to work around this defer "problem". Still - Beware of how you do defer
@Majiy00
@Majiy00 2 месяца назад
Great videos, you deserve more views, subscribed!
@FloWoelki
@FloWoelki 2 месяца назад
Wow, thank you so much 🥺
@SoyaibZihad10
@SoyaibZihad10 16 дней назад
great video. can you name the font please?
@FloWoelki
@FloWoelki 12 дней назад
Thank you! I am using the Monaspace font :)
@SoyaibZihad10
@SoyaibZihad10 8 дней назад
@@FloWoelki great. thanks for the reply.
@bravian7958
@bravian7958 3 месяца назад
which ide do you use
@FloWoelki
@FloWoelki 3 месяца назад
I am using Zed at the moment :) I've also made a quick video about Zed, if you want to check it out.
@youknownothing_
@youknownothing_ 2 месяца назад
noticed you are using zed, which theme is that?
@FloWoelki
@FloWoelki 2 месяца назад
It's the GitHub theme :)
Далее
You have to know these two Golang 1.23 changes!
16:38
Просмотров 3,3 тыс.
Mcdonalds cups and ball trick 🤯🥤 #shorts
00:25
Просмотров 1,3 млн
Level Up Your Golang: 5 Concepts You Need to know
19:22
A hands-on guide for proper Unit Testing in Go!
18:44
Просмотров 3,4 тыс.
Creating custom struct tags in Golang is awesome!
24:42
Go 1.23: Custom Iterators Explained - Best feature?!
17:43
DIY Golang Web Server: No Dependencies Needed!
25:04
This Will Make Everyone Understand Golang Interfaces
21:03
Go from Zero to Hero: Learn Golang in 15 minutes!
16:06
Cómo pensaba que sería el iPhone 16 😭
0:12
Просмотров 2,3 млн
iPad Turned Into Car Touchscreen (Via: @biggibril)
0:11
Умный обзор умного iPhone 16 / 16 Pro
21:21
Умный обзор умного iPhone 16 / 16 Pro
21:21