Go 1.22 - Fixes For Loops | Prime News

  Рет қаралды 68,836

ThePrimeTime

ThePrimeTime

8 ай бұрын

Recorded live on twitch, GET IN
/ theprimeagen
Reviewed article: go.dev/blog/loopvar-preview
Authors: David Chase and Russ Cox
MY MAIN YT CHANNEL: Has well edited engineering videos
/ theprimeagen
Discord
/ discord
Have something for me to read or react to?: / theprimeagenreact
Hey I am sponsored by Turso, an edge database. I think they are pretty neet. Give them a try for free and if you want you can get a decent amount off (the free tier is the best (better than planetscale or any other))
turso.tech/deeznuts

Пікірлер: 126
@queueue_
@queueue_ 8 ай бұрын
Just starting to get into Go, glad they're fixing this before I have to deal with it 😅
@d1ngd0
@d1ngd0 8 ай бұрын
I frankly have never ran into this…. Knowingly 😰
@MyroslavSuprun
@MyroslavSuprun 8 ай бұрын
Yeah, I am currently learning Go as well and I ran exactly into this problem yesterday and it took me almost 2 hours to solve it. Good no-one is going to experience it any more.
@miracleinnocent2649
@miracleinnocent2649 8 ай бұрын
Unless you’re writing 5 thousand lines everyday you won’t encounter this mistake more than 4 times
@yash_renaissance_athlete
@yash_renaissance_athlete 8 ай бұрын
this is a pretty chill thing to be honest. I am not very impressed or certainly happy with this fix but yeah I am just biased because recognising and catching this potential bug is on my muscle memory. But yeah, I can see how this bug could have been a mess to deal with by a person newly getting into Go.
@abhishekkumar-dd1fc
@abhishekkumar-dd1fc 8 ай бұрын
Lucky 😁
@vikramkrishnan6414
@vikramkrishnan6414 8 ай бұрын
10/10 move. The first time you are working with goroutines and encounter this, some malding occurs. Fixing this is a major win for DX
@michelians1148
@michelians1148 8 ай бұрын
Only took 16 years.
@PhilipAlexanderHassialis
@PhilipAlexanderHassialis 8 ай бұрын
Same thing happens with Tasks in C# - where you have to capture the loop variable value. It's a classic issue and a classic interview question.
@rosehogenson1398
@rosehogenson1398 8 ай бұрын
This is a great change. Almost everyone runs into this issue at least once when they're starting out.
@thomasw.4298
@thomasw.4298 8 ай бұрын
There is definitely somebody on the Go team saying "told you so" right about now. You should interview this chad and see what other ideas he wants to fix
@advertslaxxor
@advertslaxxor 8 ай бұрын
if err := Something(); err != nil { ... } blah, err := foo() Hopefully. The first case should just be: if err := Something() { ... } The second, conflicted. I think we can have another := that returns default values and the error if it fails. blah ?= foo() Translates to: blah, err := foo() if err != nil { return ..., err }
@advertslaxxor
@advertslaxxor 8 ай бұрын
Also I want to steal 'with' from python. with foo, err := Open(something) { // err is nil // implict defer foo.Close() } else { // err is not nil }
@looncraz
@looncraz 8 ай бұрын
@@advertslaxxor I want an or keyword for this : err := Something() or return err err := Something() or { log.Error("...", err) return err } Or, better yet, automatic capture of the first error-type return value or the first returned value if no error type given: Something() or return error
@Propherex
@Propherex 8 ай бұрын
1:32 You make mistake - skill issue. 2:33 I make mistake - its a bug.
@buddy.abc123
@buddy.abc123 8 ай бұрын
So happy they're fixing this before I even stumbled into it as I'm still learning Go
@enkiimuto1041
@enkiimuto1041 8 ай бұрын
Same here. I can see myself spending hours not understanding what is happening lol
@ruanpingshan
@ruanpingshan 8 ай бұрын
Same here. C# fixed this over a decade ago, so I would've assumed that Go worked the same way.
@Tresla
@Tresla 8 ай бұрын
Even after programming in Go for the past 7 years, this one still trips me up on occasion. Definitely a welcome change!
@scheimong
@scheimong 8 ай бұрын
Rust casually going: "oh it's impossible for us to have this problem by design". All hail the glorious borrow checker.
@mikereynolds1368
@mikereynolds1368 8 ай бұрын
Damn I was hoping for a Prime breakdown example of the issue, the fix, and the correct way to actually do the loop.
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars 8 ай бұрын
when learning go there was always a warning regarding this and the work around glad it's been fixed :)
@l3ss1sm0r3
@l3ss1sm0r3 8 ай бұрын
Thats why immutability by default is a thing I guess ... and feels so nice to work with imo
@EskoLuontola
@EskoLuontola 8 ай бұрын
I was complaining about that back in February 2010, that there was no reasonable code which would depend on the original behavior, but that the feature was a pure footgun. Finally they fixed it. 🎉
@hackebeil20
@hackebeil20 8 ай бұрын
yeah. still so many things unfixed since forever. I was really excited for Go at the beginning. not so much anymore after I've seen many stupid design choices and weird things in the stdlib.
@ruanpingshan
@ruanpingshan 8 ай бұрын
This takes me back to 2012, which is when C# solved this exact problem by copying the loop variable behind the scenes. Java got closures in 2014, and right from the start it's been a compiler error if you didn't copy the loop variable before capturing.
@NicholasKoukoutsis
@NicholasKoukoutsis 8 ай бұрын
This was a rollercoaster of emotions for me. At first, I PASSIONATELY thought the same - this is a skill issue. And then I had the gottem moment. 10/10 move from Go. Gotta say though, you feel pretty big brained knowing this, and telling some junior that tHe sCoPe oF a VaRiAbLe iS tHe LoOp, and bestowing that knowledge in a PR review...but getting caught in that mess is not certainly not worth it. I only hope that if there are performance implications like GC on the iteration of the loop or something, and that if there are, compiler is clever enough to use the old method if no references are passed around
@tokiomutex4148
@tokiomutex4148 8 ай бұрын
That one always catches me, can't wait for 1.22 to come out
@codephobia
@codephobia 8 ай бұрын
Great update. I would always forget about this gotcha after a while and do it again and again.
@ThePrimeTimeagen
@ThePrimeTimeagen 8 ай бұрын
agreed
@Zed_Oud
@Zed_Oud 8 ай бұрын
This is fundamentally why Python 2->3 happened: to limit or even eliminate ambiguity over a core feature.
@Yay295
@Yay295 8 ай бұрын
I haven't used Go, but I was surprised this was even a thing, considering this had been a known issue in JavaScript for years, and JavaScript fixed it in 2016 with let/const.
@narekasadorian
@narekasadorian 8 ай бұрын
Rust: standing on the shoulders of giants Go: laying in a ditch with an empty 40oz
@a-yon_n
@a-yon_n 8 ай бұрын
I can’t imagine so many people in the comments saying that they had never encountered this issue. I ran into the problem the first week I learned Go. Back then I just thought that Golang generally didn’t support scoped variables in condition statements and I had to use IIFE to avoid the problem. Glad that they admitted that it was an issue and tried to fix.
@cariyaputta
@cariyaputta 8 ай бұрын
The classic tc := tc in parallel tests.
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
I think I ran into such a bug yesterday, in C. It introduced really weird errors, segmentation faults before main starts. I should try go a little because I don't understand this code at any level. Are "func" lambdas and [] fat pointers? Is
@x0z59
@x0z59 8 ай бұрын
Yey, Go is going bigger and more popular!
@heathkuntz8638
@heathkuntz8638 8 ай бұрын
Hilarious that I ran into this very thing just two days ago.... great change.
@Sindrijo
@Sindrijo 8 ай бұрын
This is such a common 'oopsie' in many languages. This exact same issue was fixed in C# 5 almost 10 years ago.
@RandomGeometryDashStuff
@RandomGeometryDashStuff 8 ай бұрын
for _, v = range values { vcopy := v } is value copied twice (from array to v and from v to vcopy)?
@BenVisness
@BenVisness 8 ай бұрын
No, the extra copy is optimized away. You can see that this is true if you enter your example into godbolt. (KZbin won’t let me paste a link, I think, but you can Google it.) Notably, no extra copying occurs even when the variable is captured for use in a goroutine. I’d be happy to try and explain the assembly if you’re unfamiliar btw, I know it can be dense!
@BenVisness
@BenVisness 8 ай бұрын
@@anon_y_mousse I meant that the second copy is optimized away - that there would not be a copy both from the slice to v and then from v to vcopy. In my opinion a single copy is completely reasonable. I would expect the loop variable to behave like v := array[i], not v := &array[i]. The issue you’re concerned about isn’t one of copies but one of lifetime. Is the lifetime of the loop variable considered to be the lifetime of the entire loop or a single iteration? This does not affect performance as you seem to claim, but only the semantics of which memory locations will be accessed when the variable is captured by another function.
@jaysistar2711
@jaysistar2711 8 ай бұрын
You couldn't have that problem in Rust, since the variable would have to be immutable, or copied/cloned.
@softed
@softed 8 ай бұрын
Wow really no way
@To1ne
@To1ne 8 ай бұрын
I wish I was there on stream to see how happy he was with that endong
@crrodriguez
@crrodriguez 8 ай бұрын
Rust : It ain't happening under the watch of the borrow checker !
@ant1fact
@ant1fact 7 ай бұрын
Thanks for the insights.
@om3galul989
@om3galul989 8 ай бұрын
It's night and day diff when a language is being managed by experts who know wtf they're doing and not just shoving the language with shiny things. That's why I believe in Go's future, competency is beautiful isn't it.
@ddotmars
@ddotmars 8 ай бұрын
lmao so true
@SimonBuchanNz
@SimonBuchanNz 8 ай бұрын
This is literally the exact same issue that JavaScript had to fix. So basically Go is created by people just as expert as the people that made JavaScript.
@linearz
@linearz 8 ай бұрын
​@@SimonBuchanNzyeah, lol
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
It sounds like a huge milestone to be able to make GO, it sounds odd that they would be ignorant or unskilled.
@mayur9876
@mayur9876 8 ай бұрын
Lesson learned. Use rust.
@MrWorshipMe
@MrWorshipMe 8 ай бұрын
Why are they taking range and ignoring index? Is that how you're supposed to write for loops in Go?
@aaronevans6442
@aaronevans6442 8 ай бұрын
If Golang is getting close to implementing loops, they'll probably have proof of concept variables working soon.
@SimonBuchanNz
@SimonBuchanNz 8 ай бұрын
Ironically, this issue was actually due to the fact the variables are a lot more complicated than people expect.
@spacelem
@spacelem 6 ай бұрын
This reminds me of a change they made in Julia 1.0 where `x = 0; for i in 1:10 x += i end` would leave you with x==0 (instead of the expected x==55) unless you modified it to `global x = i` (otherwise inside the for loop was a new scope and a new local x). After many many complaints they later decided that you'd get the expected behaviour in the REPL, but when called from a script it would give you a warning. Just to make this even more confusing, if you wrap that code in a function, you get the expected behaviour again, which is a real hassle if you're prototyping the internals of a function in global scope. Ah Julia, love the language but that is such a horrible surprise for people new to it.
@GearsDatapacks
@GearsDatapacks 8 ай бұрын
Great change. Love this
@thingsiplay
@thingsiplay 8 ай бұрын
Is Pokemon Go written in Golang?
@nakedsquirtle
@nakedsquirtle 8 ай бұрын
Probably Java for android
@thingsiplay
@thingsiplay 8 ай бұрын
@@anon_y_mousse Not sure what the state nowadays is. But wouldn't it be a national crime to call it Go, if it is not written in Golang?
@Nenad_bZmaj
@Nenad_bZmaj 3 ай бұрын
@@thingsiplay Go is a 2500 years old famous Chinese game.
@blarghblargh
@blarghblargh 8 ай бұрын
code coverage doesn't say anything about quality of coverage. it is useful to tell you what you're entirely missing, and that's about it. more a floor than a ceiling
@dennywong2408
@dennywong2408 8 ай бұрын
For a GC-ed language like Go, wouldn't this create a variable per iteration requiring GC, as opposed to
@BenVisness
@BenVisness 8 ай бұрын
It only matters if the lifetime of the variable escapes the loop, such as in the goroutine examples. In those cases there will be more GC, yes, but you wanted that anyway because you wanted each goroutine to have its own variables.
@Nenad_bZmaj
@Nenad_bZmaj 3 ай бұрын
@@BenVisness But when it doesn't escape, and your loop has 100 million iterations, how big the stack grows?
@vaniusrb
@vaniusrb 8 ай бұрын
"...even the expert of experts at Go can't write it correctly". That was epic! What is the cost of a bug in production? Maybe dealing with Rust's difficult mental model will be worth it in the end, who knows.
@Nenad_bZmaj
@Nenad_bZmaj 3 ай бұрын
And what if I don't use go routines and don't want to modify the element of slice, but just use it as a source of values? With the old compiler I'd write: type record struct { Name string Age int } func main() { ages := []int{15, 20, 25, 40} names := []string{"John", "Mary", "Peter", "Sophie"} records := make([]record, len(names)) for i := range names { records[i].Age = ages[i] records[i].Name = names[i] } for _, rec := range records { fmt.Println(rec) } } and the iteration vars 'i' and 'rec' were reused in each iteration. Now they get saved in each iteration. This is BAD. In the best scenario for the second loop, the new compiler instructs the runtime to save only index iteration var and to use it as a reference to the element in the array. But this can still be 100 milion of ints saved per loop, if the slice is that long.
@edantas
@edantas 8 ай бұрын
Ah the classic headache that every go beginner goes through
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
I trust that, but it sounds odd that it's a Go thing. Do other languages help better? I've only written parallel stuff in C for Uni.
@hamzzak
@hamzzak 8 ай бұрын
someone in the chat: Go is great, but the more it goes the better it is, it's a fine wine lmao
@theohallenius8882
@theohallenius8882 8 ай бұрын
I just got into go... what did I get myself into..
@aziz0x00
@aziz0x00 8 ай бұрын
someone else uploaded this while Prime is live on twitch actually?
@DryBones111
@DryBones111 8 ай бұрын
I'm ashamed to admit that I've never considered what the scope of a "foreach" variable is. I certainly didn't consciously consider it to be the same as the indexer of a typical "for" loop. Now I am a tiny bit wiser.
@llIlllIIlIIlllIlllIl
@llIlllIIlIIlllIlllIl 8 ай бұрын
Each language has them differently, so make sure you consult your language's documentation. (I read in the HN discussion of this post that C# changed its scope as they updated the language.)
@llIlllIIlIIlllIlllIl
@llIlllIIlIIlllIlllIl 8 ай бұрын
@@anon_y_mousse Yes, I meant something similar. Put succintly the scope of the iterating variable may or may not equal to the scope of the iterator.
@llIlllIIlIIlllIlllIl
@llIlllIIlIIlllIlllIl 8 ай бұрын
@@anon_y_mousseOhh my bad I meant to say something like iterable collection, instead of iterator...
@llIlllIIlIIlllIlllIl
@llIlllIIlIIlllIlllIl 8 ай бұрын
Also re: the "copies the value it references" happens on variable declaration (which is why scope matters), rater than using the variable inside a closure, no? If you think about it a variable of type T represents a reference to T.
@asdfghyter
@asdfghyter 7 ай бұрын
this is an excellent change and how all languages should do it. this is how humans would intuitively expect it to work anyways and there is really no use case where you would need the old behaviour
@sohn7767
@sohn7767 8 ай бұрын
Very cool, about time languages fix this mistake… lots of other languages still suffer from the same fate and will continue for to do for eternity
@user-sv3dc5nz8w
@user-sv3dc5nz8w 8 ай бұрын
Deal. I will learn some Go stuff this weekend. No goshamig anymore.
@magne6049
@magne6049 8 ай бұрын
2:25 it's funny how it's not a skill issue once oneself is involved ;-)
@hackebeil20
@hackebeil20 8 ай бұрын
"go is becoming a really good modern language" - true. my only gripe is: why did they have to repeat all the stupid mistakes from other languages first? things like generics and package management existed way before golang. They could have just started with that right off the bat. Same goes for proper error handling, logging, flag parsing, and being able to return an effing return code from main().
@jitx2797
@jitx2797 8 ай бұрын
I never knew this existed. Damn I ain't creating anything good...
@yurcchello
@yurcchello 8 ай бұрын
CHRO-O-O-O-T!
@complexity5545
@complexity5545 8 ай бұрын
All of this is starting to look like perl and other languages had figured out 18 years ago. It amazes me that the same problems in everyone's new features and newer programming languages keep repeating the same mistake over and over again. After I punted Rust, I am back into C++ crazy template stuff. Same mistakes in all language designs.
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
I feel this way about CSS scoping, when we've got Unix. Also today's editors and Emacs. Like people want to add, not preserve.
@hereallyfast
@hereallyfast 8 ай бұрын
IN foooor MA!
@IvanKravarscan
@IvanKravarscan 8 ай бұрын
Min and max on any type of a number, eh, even Kotlin doesn't have that...
@nexovec
@nexovec 8 ай бұрын
What the frick, I had to open my go program because I had this bug there.
@Im_Ninooo
@Im_Ninooo 8 ай бұрын
W change
@nitsugaorom1091
@nitsugaorom1091 8 ай бұрын
coconut oil
@u9vata
@u9vata 8 ай бұрын
This indeed seemed like pretty bad design from Go originally. I honestly prefer writing out indexed for look though and many people not understand why: why? Because in all random languages always harder to fuck it up - both perf-wise and semantics-wise and also known to everyone from first sight.
@window.location
@window.location 8 ай бұрын
Absolute based move by Go team, can't wait for 1.22
@Diego-Garcia
@Diego-Garcia 8 ай бұрын
The name is Thebuggygen
@vitorguidorizzzi7538
@vitorguidorizzzi7538 8 ай бұрын
1.22 over 1.0 fixing for loops lmao
@rewplaypark
@rewplaypark 8 ай бұрын
Glad to see this get addressed. The reason why this is such the pain is that Go doesn't have Optional type. We need to use things like *int to represent Optional type so it become abundant in the code and hard to catch on the fly.
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
Wait, people make integers be garbage collected so they can return Null? I haven't tried GO but that's what I get
@rewplaypark
@rewplaypark 8 ай бұрын
@@theodorealenas3171 basically yes, especially when interacting with ORM, JSON serializing, etc.
@rickdg
@rickdg 8 ай бұрын
So I guess that the “Go is boring” arc is over now.
@elderofzion
@elderofzion 2 күн бұрын
i use go, rust to difficult for me
@Alex-hr2df
@Alex-hr2df 8 ай бұрын
Sorry Rust, but Go is the most practical language for BE, hands down.
@hakuna_matata_hakuna
@hakuna_matata_hakuna 8 ай бұрын
Your excitement about go seems like a bit you've managed to commit to for this long
@anybody4802
@anybody4802 8 ай бұрын
please somebody explain
@janAkaliKilo
@janAkaliKilo 8 ай бұрын
array = [1, 2, 3] for i in array: list.add(closure that uses i) closures in the list == (3, 3, 3) You expect to see different i for each item in array, but clojures close over variables and not values. So every closure you added to the list - remembers the i variable, and not the value. How go fixed this: now address of 'i' is unique per iteration, not per loop, so every closure will see the unique variable, that won't change with next iteration.
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
​@@janAkaliKilowhat I don't get is, isn't the counter a primitive type? Why is it passed by reference?
@janAkaliKilo
@janAkaliKilo 8 ай бұрын
@@theodorealenas3171 because it is unique behaviour of closures. They never explicitly copy values, closures keep the reference to the address of variable. That is practically the definition of closure.
@theodorealenas3171
@theodorealenas3171 8 ай бұрын
@@janAkaliKilo oh I see. I thought closure and lambda are synonyms.
@johannes596
@johannes596 8 ай бұрын
Honestly, pretty embarrassing to create a NEW language with such a flaw. Wow. Great job, Go.
@morphles
@morphles 8 ай бұрын
As I said in more than one video, I thing go is just bad language. And that such crap can happen, is just another stone in their garden. Well eventually in like 10 years they might become decent, just look at PHP, it's now IMO one of the better languages at least by DX. PHPUnit for testing is hands down best thing I actually used for testing, symfony debug stuff is quite ahead of say nextjs. Exceptoins and fail fast is super nice when you get good stack traces. As opposed to errors that provide no traces, or god forbid you missed that function returns error, and it occurs, you are in for some real fun time.
@miracleinnocent2649
@miracleinnocent2649 8 ай бұрын
So ????
@lungfish
@lungfish 8 ай бұрын
Would not happen in Rust
@Nenad_bZmaj
@Nenad_bZmaj 3 ай бұрын
Fixing the proper language just to let ignorant speakers talk, or: a new auto-correct.
@user-tv4fu9wd7u
@user-tv4fu9wd7u 8 ай бұрын
c programmer: so weak
@Tony-dp1rl
@Tony-dp1rl 8 ай бұрын
Go is such an ugly language for a new creation. I mean, did they really need to make the syntax that ugly in order to get the same amazing features?
@zhamed9587
@zhamed9587 8 ай бұрын
golang is such a badly designed language. Now fix generics, interfaces, null pointers, add immutable types, add pattern matching with exhaustiveness checks, proper error handling, and then maybe it can be worth looking into
@a-yon_n
@a-yon_n 8 ай бұрын
Others may be optional, but generics is a pain in the ass in Golang.
@annoorange123
@annoorange123 8 ай бұрын
What is "proper error handling" for you?
@zhamed9587
@zhamed9587 8 ай бұрын
@@annoorange123 Being able to compose/chain functions that return errors easily. Having errors that are not strings. Errors that are not easily ignored by mistake.
@vanillaface6097
@vanillaface6097 8 ай бұрын
Golang - the trash of programming languages which is being praised for new features everyone else had ages ago.
@JorgeMartinez-xb2ks
@JorgeMartinez-xb2ks 8 ай бұрын
C@@L
@user-kw9cu
@user-kw9cu 8 ай бұрын
LETS GO LETS GO ELSETS SGO GL ELTSET LETS GOOOOOOOOOOOOOOOOO
Season 4 | Episode 4 - Minconsult Sdn Bhd ft. CEO Dato' Dr Dennis Ganendra
22:39
The Shift Asia - Sustainability Stories
Рет қаралды 324
когда достали одноклассники!
00:49
БРУНО
Рет қаралды 3,9 МЛН
MOM TURNED THE NOODLES PINK😱
00:31
JULI_PROETO
Рет қаралды 22 МЛН
Go: 1 Thing I Would Change
15:02
ThePrimeTime
Рет қаралды 54 М.
Optimizing Loops In Go | Prime Reacts
10:34
ThePrimeTime
Рет қаралды 80 М.
Become A Microsoft Copilot Pro With This Simple Tutorial!
10:21
AI Joyful Discoveries
Рет қаралды 1 М.
The standard library now has all you need for advanced routing in Go.
13:52
This CLI Tool is AMAZING | Prime Reacts
11:59
ThePrimeTime
Рет қаралды 77 М.
The Vlang Drama
43:35
ThePrimeTime
Рет қаралды 95 М.
Building REST APIs in Go 1.22 - New Features
7:10
TutorialEdge
Рет қаралды 20 М.
New to Go Team Takes Down Prod | Prime Reacts
13:25
ThePrimeTime
Рет қаралды 112 М.
Go 1.20 Memory Arenas Are AMAZING | Prime Reacts
16:38
ThePrimeTime
Рет қаралды 95 М.
Why Kotlin Is The Best Language (to use with htmx)
20:54
ThePrimeTime
Рет қаралды 103 М.
What model of phone do you have?
0:16
Hassyl Joon
Рет қаралды 75 М.
cool watercooled mobile phone radiator #tech #cooler #ytfeed
0:14
Stark Edition
Рет қаралды 7 МЛН
POCO F6 PRO - ЛУЧШИЙ POCO НА ДАННЫЙ МОМЕНТ!
18:51
wireless switch without wires part 6
0:49
DailyTech
Рет қаралды 1 МЛН
Main filter..
0:15
CikoYt
Рет қаралды 756 М.
Задача APPLE сделать iPHONE НЕРЕМОНТОПРИГОДНЫМ
0:57