Тёмный

Go 1.22 - Fixes For Loops | Prime News 

ThePrimeTime
Подписаться 614 тыс.
Просмотров 70 тыс.
50% 1

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

 

22 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 120   
@queueue_
@queueue_ Год назад
Just starting to get into Go, glad they're fixing this before I have to deal with it 😅
@d1ngd0
@d1ngd0 Год назад
I frankly have never ran into this…. Knowingly 😰
@MyroslavSuprun
@MyroslavSuprun Год назад
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 Год назад
Unless you’re writing 5 thousand lines everyday you won’t encounter this mistake more than 4 times
@yash_renaissance_athlete
@yash_renaissance_athlete Год назад
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.
@Nenad_bZmaj
@Nenad_bZmaj 8 месяцев назад
When you learn Go without this ''fix'', you shall not make these stupid mistakes, anyway.
@vikramkrishnan6414
@vikramkrishnan6414 Год назад
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 Год назад
Only took 16 years.
@PhilipAlexanderHassialis
@PhilipAlexanderHassialis Год назад
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 Год назад
This is a great change. Almost everyone runs into this issue at least once when they're starting out.
@thomasw.4298
@thomasw.4298 Год назад
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 Год назад
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 Год назад
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 Год назад
@@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
@siya.abc123
@siya.abc123 Год назад
So happy they're fixing this before I even stumbled into it as I'm still learning Go
@enkiimuto1041
@enkiimuto1041 Год назад
Same here. I can see myself spending hours not understanding what is happening lol
@ruanpingshan
@ruanpingshan Год назад
Same here. C# fixed this over a decade ago, so I would've assumed that Go worked the same way.
@Tresla
@Tresla Год назад
Even after programming in Go for the past 7 years, this one still trips me up on occasion. Definitely a welcome change!
@scheimong
@scheimong Год назад
Rust casually going: "oh it's impossible for us to have this problem by design". All hail the glorious borrow checker.
@Propherex
@Propherex Год назад
1:32 You make mistake - skill issue. 2:33 I make mistake - its a bug.
@EskoLuontola
@EskoLuontola Год назад
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 Год назад
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.
@mikereynolds1368
@mikereynolds1368 Год назад
Damn I was hoping for a Prime breakdown example of the issue, the fix, and the correct way to actually do the loop.
@ruanpingshan
@ruanpingshan Год назад
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.
@codephobia
@codephobia Год назад
Great update. I would always forget about this gotcha after a while and do it again and again.
@ThePrimeTimeagen
@ThePrimeTimeagen Год назад
agreed
@Zed_Oud
@Zed_Oud Год назад
This is fundamentally why Python 2->3 happened: to limit or even eliminate ambiguity over a core feature.
@l3ss1sm0r3
@l3ss1sm0r3 Год назад
Thats why immutability by default is a thing I guess ... and feels so nice to work with imo
@NicholasKoukoutsis
@NicholasKoukoutsis Год назад
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
@a_maxed_out_handle_of_30_chars
when learning go there was always a warning regarding this and the work around glad it's been fixed :)
@narekasadorian
@narekasadorian Год назад
Rust: standing on the shoulders of giants Go: laying in a ditch with an empty 40oz
@Yay295
@Yay295 Год назад
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.
@a-yon_n
@a-yon_n Год назад
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.
@spacelem
@spacelem 11 месяцев назад
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.
@tokiomutex4148
@tokiomutex4148 Год назад
That one always catches me, can't wait for 1.22 to come out
@asdfghyter
@asdfghyter Год назад
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
@om3galul989
@om3galul989 Год назад
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 Год назад
lmao so true
@SimonBuchanNz
@SimonBuchanNz Год назад
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 Год назад
​@@SimonBuchanNzyeah, lol
@theodorealenas3171
@theodorealenas3171 Год назад
It sounds like a huge milestone to be able to make GO, it sounds odd that they would be ignorant or unskilled.
@cariyaputta
@cariyaputta Год назад
The classic tc := tc in parallel tests.
@Sindrijo
@Sindrijo Год назад
This is such a common 'oopsie' in many languages. This exact same issue was fixed in C# 5 almost 10 years ago.
@DryBones111
@DryBones111 Год назад
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.
@theodorealenas3171
@theodorealenas3171 Год назад
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
@RandomGeometryDashStuff
@RandomGeometryDashStuff Год назад
for _, v = range values { vcopy := v } is value copied twice (from array to v and from v to vcopy)?
@BenVisness
@BenVisness Год назад
No, the extra copy is optimized away. You can see that this is true if you enter your example into godbolt. (RU-vid 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 Год назад
@@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.
@blarghblargh
@blarghblargh Год назад
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
@x0z59
@x0z59 Год назад
Yey, Go is going bigger and more popular!
@jaysistar2711
@jaysistar2711 Год назад
You couldn't have that problem in Rust, since the variable would have to be immutable, or copied/cloned.
@softed
@softed Год назад
Wow really no way
@sohn7767
@sohn7767 Год назад
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
@vaniusrb
@vaniusrb Год назад
"...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.
@aaronevans6442
@aaronevans6442 Год назад
If Golang is getting close to implementing loops, they'll probably have proof of concept variables working soon.
@SimonBuchanNz
@SimonBuchanNz Год назад
Ironically, this issue was actually due to the fact the variables are a lot more complicated than people expect.
@heathkuntz8638
@heathkuntz8638 Год назад
Hilarious that I ran into this very thing just two days ago.... great change.
@edantas
@edantas Год назад
Ah the classic headache that every go beginner goes through
@theodorealenas3171
@theodorealenas3171 Год назад
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.
@crrodriguez
@crrodriguez Год назад
Rust : It ain't happening under the watch of the borrow checker !
@dennywong2408
@dennywong2408 Год назад
For a GC-ed language like Go, wouldn't this create a variable per iteration requiring GC, as opposed to
@BenVisness
@BenVisness Год назад
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 8 месяцев назад
@@BenVisness But when it doesn't escape, and your loop has 100 million iterations, how big the stack grows?
@mayur9876
@mayur9876 Год назад
Lesson learned. Use rust.
@thingsiplay
@thingsiplay Год назад
Is Pokemon Go written in Golang?
@ոakedsquirtle
@ոakedsquirtle Год назад
Probably Java for android
@thingsiplay
@thingsiplay Год назад
@@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 8 месяцев назад
@@thingsiplay Go is a 2500 years old famous Chinese game.
@ant1fact
@ant1fact Год назад
Thanks for the insights.
@To1ne
@To1ne Год назад
I wish I was there on stream to see how happy he was with that endong
@hackebeil20
@hackebeil20 Год назад
"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().
@MrWorshipMe
@MrWorshipMe Год назад
Why are they taking range and ignoring index? Is that how you're supposed to write for loops in Go?
@Nenad_bZmaj
@Nenad_bZmaj 8 месяцев назад
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.
@hamzzak
@hamzzak Год назад
someone in the chat: Go is great, but the more it goes the better it is, it's a fine wine lmao
@complexity5545
@complexity5545 Год назад
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 Год назад
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.
@u9vata
@u9vata Год назад
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.
@GearsDatapacks
@GearsDatapacks Год назад
Great change. Love this
@theohallenius8882
@theohallenius8882 Год назад
I just got into go... what did I get myself into..
@aziz0x00
@aziz0x00 Год назад
someone else uploaded this while Prime is live on twitch actually?
@magne6049
@magne6049 Год назад
2:25 it's funny how it's not a skill issue once oneself is involved ;-)
@IvanKravarscan
@IvanKravarscan Год назад
Min and max on any type of a number, eh, even Kotlin doesn't have that...
@rickdg
@rickdg Год назад
So I guess that the “Go is boring” arc is over now.
@Alex-hr2df
@Alex-hr2df Год назад
Sorry Rust, but Go is the most practical language for BE, hands down.
@jitxhere
@jitxhere Год назад
I never knew this existed. Damn I ain't creating anything good...
@nexovec
@nexovec Год назад
What the frick, I had to open my go program because I had this bug there.
@rewplaypark
@rewplaypark Год назад
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 Год назад
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 Год назад
@@theodorealenas3171 basically yes, especially when interacting with ORM, JSON serializing, etc.
@Diego-Garcia
@Diego-Garcia Год назад
The name is Thebuggygen
@hereallyfast
@hereallyfast Год назад
IN foooor MA!
@yurcchello
@yurcchello Год назад
CHRO-O-O-O-T!
@Im_Ninooo
@Im_Ninooo Год назад
W change
@window.location
@window.location Год назад
Absolute based move by Go team, can't wait for 1.22
@nitsugaorom1091
@nitsugaorom1091 Год назад
coconut oil
@vitorguidorizzzi7538
@vitorguidorizzzi7538 Год назад
1.22 over 1.0 fixing for loops lmao
@elderofzion
@elderofzion 4 месяца назад
i use go, rust to difficult for me
@hakuna_matata_hakuna
@hakuna_matata_hakuna Год назад
Your excitement about go seems like a bit you've managed to commit to for this long
@johannes-vollmer
@johannes-vollmer Год назад
Honestly, pretty embarrassing to create a NEW language with such a flaw. Wow. Great job, Go.
@anybody4802
@anybody4802 Год назад
please somebody explain
@dolorsitametblue
@dolorsitametblue Год назад
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 Год назад
​@@dolorsitametbluewhat I don't get is, isn't the counter a primitive type? Why is it passed by reference?
@dolorsitametblue
@dolorsitametblue Год назад
@@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 Год назад
@@dolorsitametblue oh I see. I thought closure and lambda are synonyms.
@lungfish
@lungfish Год назад
Would not happen in Rust
@morphles
@morphles Год назад
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 Год назад
So ????
@Nenad_bZmaj
@Nenad_bZmaj 8 месяцев назад
Fixing the proper language just to let ignorant speakers talk, or: a new auto-correct.
@zhamed9587
@zhamed9587 Год назад
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 Год назад
Others may be optional, but generics is a pain in the ass in Golang.
@annoorange123
@annoorange123 Год назад
What is "proper error handling" for you?
@zhamed9587
@zhamed9587 Год назад
@@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.
@Tony-dp1rl
@Tony-dp1rl Год назад
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?
@นวคุณ-ส9ห
@นวคุณ-ส9ห Год назад
c programmer: so weak
@vanillaface6097
@vanillaface6097 Год назад
Golang - the trash of programming languages which is being praised for new features everyone else had ages ago.
@JorgeMartinez-xb2ks
@JorgeMartinez-xb2ks Год назад
C@@L
@user-kw9cu
@user-kw9cu Год назад
LETS GO LETS GO ELSETS SGO GL ELTSET LETS GOOOOOOOOOOOOOOOOO
Далее
JavaScript Is Becoming 2 Languages?? FROM TC39
30:01
Просмотров 13 тыс.
Go vs Rust vs Bun vs Node | Prime Reacts
18:07
Просмотров 173 тыс.
This CLI Tool is AMAZING | Prime Reacts
11:59
Просмотров 81 тыс.
Glove80 Pinks vs. Cherry Blossom switches
0:21
Просмотров 1,1 тыс.
Go 1.20 Memory Arenas Are AMAZING | Prime Reacts
16:38
When Zig Outshines Rust | Prime Reacts
23:31
Просмотров 142 тыс.
Optimizing Loops In Go | Prime Reacts
10:34
Просмотров 83 тыс.
err != nil Is GOOD? (And Why)
7:19
Просмотров 93 тыс.
.io Domains Are Going Away??
18:13
Просмотров 92 тыс.