Golang 1.22: What is new?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Mario welcome to another Go video in today's episode I'm sharing with you what is new in go 1.22 go 1.22 was released on February 6 2024 there are packages already available official ones for Windows Linux and Mac OS let's talk about the changes that I think are important to call out as us the link to the code for all of these examples is in the description of this video so feel free to check that out the first one new feature is the one applicable to four that fixes a gotcha that typically happens if you're unfamiliar with how go routines and the for Loop works so if you're trying to do something like this you will imagine that what is going to be printing is ABC but what is happening is that this variable that we are referring to in the gold routine is actually going to be pointing to the last value so it's going to be all we see what is going to happen now in Go 1.2 is that that is fixed behind the scenes so you don't have to explicitly do something like when you need it sort of like a defining a new variable that point points to the value that you are using in the four range so if we go and print out that uh by compiling the binary you will notice now it's working as expected one important thing to call out here is this L closure warning that is coming out from go pleas this is going to be resolved in the next version 1.5.0 please just ignore it for now it will be fixed by the go team let's go to the next new feature next feature is also related to the four statement and this adds a way to iterate through integer so the way it works is just literally pass in the integer that you want to use as a range and then it will Loop through the values this is sort of like we're doing a range of I SLI so if you go and compile this we can see that it's going to be going from 0 to four is not inclusive of the number that you're using it's an interesting and you know quite neat new feature let's go to the next one this new feature is the inclusion of the first V2 package which is an update of the math Rand package it fixes a few things that were in the previous implementation it makes it more idiomatic and it actually improves its performance let me show you a few changes typically it will be sort of like kind of backwards compatible to the code that you currently have the way it works is that actually it uses a bit of generics and a few of the new functions like I was saying in the beginning is that some of the method names or function names are much more idiomatic so for example in the V1 version it was in31 now in the V2 version will be in32 in the end it's sort of more or less the same thing what is nice and interesting is that now run. n that does a new function that receives generic with any numeric value it will give you any random number that you pass in as an argument so if we go and compile this you will see what I mean so in this case you will see that is now is running 1.8 seconds if we run it again it will give you another value between the input argument that I mentioned and again obviously it's running the random uh values in the slid as well so again it's a nice Improvement it will be nice for us to upgrade and what they have mention in the change lock is that it's going to be a new tool for eventually migrate your existing code that you have in rant in the V1 version to the v21 so let's see in the near future let's go to the fourth new feature the next feature includes a change in the server MOX type in net HTTP let me show you so this is a nice and interesting way to do things now because probably you won't need any other routers that were available before like gorilla M or chai or those kind of things what this adds is this a way to define routes that you can specify the method as well as the path with some variables or arguments that you can pass in in the route one thing that is interesting is that you can also you can pull those values from the path value like you will expect but also it allows you to verify if you have routes that have happen to be conflicting with each other they will Panic immediately and one of the cool things is that you are explicitly setting the method in advance like the one I'm defining here will be forget and and the one below is also forget but you can specify for post patch and all the other method let's compile this and I'll show you how it works so right now if I run main if you go back to the code you will see that I have a get an ID value so if I do a Let Me exit this and do a curl ID value let me change to get and my ID is ID if I change it to I don't know 100 you will see that ID is 100 now obviously if I use a method that is not defined it will give me a 404 or more importantly if I try to do what I told you before about defining a route that happens to be a similar path it will immediately Panic so if we go and compile this one more time and we run it you will see that now well it's is compiling because the values that I have here for for path get Val one and Val two kind of conflicts with the one that I have for ID and value so this is really cool it's most likely eventually you know people will start I guess it depends on project if your project is is already so dependent on something like gorilla Mo or chai well it's going to be a little bit complicated to migrate to this new implementation of server Mo but I guess for future projects it does make a lot of sense let's talk about the next feature the next feature includes embedding files in some of the fil like methods in net HTTP for example let me show you we have this code right here that it does is embedding using an API that was added a few versions ago to embed the files directory so if I go and open the files directory you will see that I have a bunch of txt files nothing really complicated so what is going to happen is when building the binary we can actually serve those files from the binary itself so let's build this example if I go back into a curl get Local Host you will notice that I have a files file if I do a files you will see that now I have the three files that I listed before I have one txt I will get the value if I do a two I will get a value one important thing is that because obviously is embedded into the binary if I go and modify one of the files and I do another curl for that spefic specific file it won't refer to the modified value but are to always the one that is embedded in the binary so this is a cool thing to serve files maybe you want to serve your Swagger documentation directly from your binary itself now you can do it in a different way when embedding files directly let's jump into the last feature that I want to call out the last feature will be a new function in the slies package that is called concat and as you may imagine it does does what it says it concatenates different slices it's a generic function that you can use with any slice type so in this case it's going to be receiving these three slices and it's going to be printing out the value of those as a new slice so if you go and compile this and we run it you will see what I mean which in this case is just concatenating three of them and returning a new value so that's it those are the six new features that I wanted to call out for go 1.22 of course there are more and I highly encourage you to read the documentation and the block post as usual there is also a link in the description of this video that includes a link to my Vlog that gives you a text version of this video thank you for watching I will talk to you next time take care stay safe see you
Info
Channel: Mario Carrion
Views: 3,827
Rating: undefined out of 5
Keywords: golang tutorial for beginners, golang tutorial, golang tutorial for begginers, golang, golang netip, learn golang, golang for beginners, golang security, golang toolchain, golang 1.21 toolchain, golang 1.21 sync, golang 1.22, golang 1.22 routing patterns, go 1.22, golang 1.22 slices, golang 1.22 changes, golang 1.22 features, golang 1.22 for gotcha, golang 1.22 tutorial, golang 1.22 alpine, go lang 1.22
Id: z_deczyFaxw
Channel Id: undefined
Length: 7min 44sec (464 seconds)
Published: Mon Feb 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.