Exciting Go Update - v1.22 Change Log With Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi and welcome to programming Percy and I am super excited because we are seeing the release of go 1.22 and it has some really amazing changes that I am so excited about I just had to make this video about it so let's start talking about go go really changed my life as a developer it's a language that really just kind of resonates with me especially the idea behind keeping things simple when I'm developing in go everything is feels easy natural and uncomplex and it really allows me as a developer to maintain a high development speed not only is it an easy language it's simple to understand and it's also really performant despite having a garbage collector so now we finally have version 1.22 and it's going to be amazing and there is some really long awaited features so let's stop beating around the bush and let's talk about the elepants so if you still haven't updated you should really go ahead and do that and when we're talking about updating go I really want to mention go tip it wasn't really that long ago that learned about go tip and it's a tool that can help you upgrade your current version of goine so what you want to do is you want to go ahead and in and install that so let's go ahead and do go.org download go tip and let's get the latest go tip version go tip really makes the process of updating your current Go version to a super easy experience let's go ahead and take a look what we do when it's installed is that you do go tip download and it's downloading and updating your version of go this can really be used to update outside of the of the releases so you can see preview versions Etc using go tip so you have to be cautious of that you can't just go ahead and do go tip all the time because you will get versions that still might not be in the current release phas so one of the coolest things that is happening in go 1.22 is that the broken four Range Loop is finally going to die and when I say broken I don't mean that it's broken it's just I think it's safe to say that the implementation of the for Loop has confused a lot of developers that are using go and I think it's safe to say that it stands for countless amounts of bugs that has been appearing in the history of go in fact fact let encrypt had to revoke 3 million certificates because of this bug and I keep seeing it appear over and over and over and it's so nice that it's finally going to die so let's take a look at the bug so you understand what it is and what I mean by it so we're going to create a super simple go program and it's just going to be a simple main function and in this main function we're going to have a list of names names so go ahead and do Percy and we have a gopher and we have Mr Santa can't have a for Loop Without Santa so let's go ahead and range over this name list and what's going to happen here is that we're going to mimic a real life application now normally if you do this there's no problem if you do it like this there's no problem but whenever you're going to use a closure or a go routine inside a for loop we're going to have some weird behaviors so let's just go ahead and create a go routine or a closer closure and this is not uncommon because when you're doing concurrent processing of a list H this is pretty common and this is when some nasty things starts happening so let's go ahead and what we're going to do is we're just going to print the names so we're going to let's make that names my bad so we have a list of names we're going to Loop over the names and we're going to print them and down here we're going to sleep now if you have a decent editor you can see I actually get a warning here saying that the variable is captured by the function L literal and what this means is that we're using a g or a closure here and this variable is not going to do what we think it is so when you start out using go you might expect this to kind of print all the names you would assume so right but let's go back to our terminal and let's execute this let's execute this program and you will see it actually prints Santa three times but that's not what we wanted to do right and this is very common and finally it's going to be fixed now before we move on I really feel that we need to explain why this happens so I'm going to give it a simple try to explain so what we have is we have a for Loop running and that for Loop is setting the variable V to the value of the name right so the value here is is getting the value from the current name and since we're doing this in a go routine we're triggering a go routine here so the variable V is actually sent to the internal scheduler in go it's not being executed right away it's sent to a q before this go routine is picked up and executed by the go routine the variable V is going going to be overwritten so whenever it's time for these functions to trigger for Loop will already have iterated all the names so we're always going to see the last name being printed so basically what we're doing is oh hey I want to start a go routine and I want you to print the N the the variable but it's going to take some time before that is executed and when it finally is is it will print the variable but the variable will have changed to the latest iteration of the for Loop and this has confused so many developers you probably have made this mistake I have made this mistake a lot of times because it's not always that easy to kind of see it happen luckily we have these great editors that now usually tell us when we're doing this but when we migrate to go 1.22 you will no longer have this issue we can actually go ahead and do I'm going to change my Go version so I'm using an old version of go right now I'm going to choose a new environment and I'm going to use go tip which now is go 1.22 you will actually see I'm no longer getting a warning so if you're using go tip um you might need to use the go tip command to run go go instead so you could do go tip run main.go for instance I do have an alas for go tip so that my go command is equal to go tip um however just download the newed version of go and you can go ahead and try it so do gun main.go you will see that we're getting Santa Percy and gopher finally it's fixed we won't see that nasty bug appear anymore now you might be wondering what about the comp compability you don't have to worry about that because projects using versions before go 1.21 will actually not attempt to compile code with modules older than 1.22 so it won't break you just have to consider that um in the future and this change is Major and it will prevent a ton of bugs and I'm so happy we're finally seeing it so what's more it's it's going to get even better so we are finally getting better HTTP routing in the standard library in Gold the standard Library goes a really long way and there's kind of this mentality that we don't need third party libraries we can just do everything our own and I feel that it's usually true for most cases but HTTP Library there can be improvements and due to this lack of functionality usually people use third party libraries to handle HTTP such as gorilla or gin or or uh Echo and others uh there's a bunch of them um and one of the big missing features is the ability to do method routing and like path parameters Etc but finally the HTTP serve Marx is getting a huge update and it's actually going to start accepting path parameters and we're going to be able to set the HTTP methods that are allowed Etc but instead of talking let's just go ahead and I want to show you the old way of doing it uh compared to the new way of doing it after go 1.22 it's going to be amazing so let's go ahead let's jump back to the code I'm actually going to remove this because we will never have to try the nasty for Loop because it's just going to be amazing in the future let's create a new HTTP serve MOX from the standard library and we're going to create a simple Handler function function that accepts hello on the Hello route and we're going to be testing how in the standard Library you would for instance only allow HTTP get methods uh on your endpoint so we have our SLO endpoint and we're going to need a function and this function is going to have to be the same as HTTP do method get and this is kind of the way that you had to control your endpoints so you weren't getting like post messages on your get end points Etc and let's return a status method not allowed so this is usually how I would write my simple end points and as you can see it kind of gets messy so let's just return a bite array that returns hello we're also going to Run This Server so let's do if error HTTP listen and serve we're going to host this on Port 880 and we're going to use the MOX as input and we're going to check if the error isn't nil and if it is we're just going to panic this is just a demo application we can do that just fine so this is how I would do method routing using the old serm and it's not a lot of code but still it's it's too much code right it's um it it kind of gets messy what we can do just to try it out let's go ahead and I'm going to open a new terminal and I'm going to name it curve just because it's fun and I'm going to run the go program again so it's running the HTTP server let's open a new terminal and let's curl it and say hello and oh my bad let's curl it on port 8080 and we should receive a hello and if I instead send a post message I will be getting no response let's add a verbos and we will see 405 method not allowed but this is one way of doing it and this is how we did it before but in go 1.22 we can remove all this code we're just going to highlight this we're going to remove it because we no longer need it in go 1.22 the serve marks accepts a single method that is allowed on the route before the route and they made it kind of clever because they don't want to change the handle Funk signature because that would break programs but the first value is a string so with a little bit of clever parsing we can actually do this now we can tell it that we're only allowing get methods on the Hello route this is much better much smoother and it actually works exactly the same so let's go ahead and restart the go program I'm going to jump back to curve I'm going to send a post and I'm getting the same message back I can do a regular curl and it prints hellos so that's a major Improvement according to me we can remove a lot of code just to handle something as trivial as a method route so another great thing in the new version of go is that we're going wild getting Wild Card patterns in the route so that will allow us to add path parameters to the URLs that we're hosting our apis on and that's going to be a giant change because path parameters are probably one of the big reasons why I see people want to use third party routers because uh it's pretty common in API that you allow path parameters if you're unsure what a path parameter is basically a value in the URL that you can add so if we have get hello and we want to allow our end point to say low to a username for instance we can allow you to pass the username as a value in the URL so you would do SL hello SL pery and it would say hello Percy and that's a path parameter and it's pretty common so let's go ahead and take a look at how you would do a path parameter in the old version um of go so we would have our path to SL hello don't forget the final slash because that's that's really important we will cover wise later so once we have that we need to grab the URL first and jl. path path then when we have the path we need to split it in the segments of the URL so we have the HTTP SL so that's two and then we have the domain name SL hello and then we have the final slash with the that contains the name so we need to separate it on slash so let's have a parts array that we get from strings. spit which will take in the path and we will split it on the separator and once we have the paths we need to actually make sure that we have the correct length on the path so the user actually provided a name so let's just do if length if length is less than three we want to return an HTTP error and we want to write invalid request and it's going to be a bad request say and let's return and then in the end we need to grab the correct part which will be index two in our case and then we need to add the name to to the output so let's do Sprint F hello string and let's place the name variable there so this is how you did it in the old HT pouter and it's kind of long to grab a single value and that's messy so let's go ahead and restart the go program and let's curve hello Percy and you will see that it grabs the value from the URL and puts it in the message so that's a path parameter and it's messy but the new serve MOX is amazing so it's going to allow us to automatically do everything that we're doing here but in a much better way okay let's just go ahead let's remove all this code and remember in a in a old version you might have wanted to check if it's a get message and you would have all the previous code as well it would get kind of long but now we can do get hello and we can use the braces here and say we're going to grab a variable named name from this section of the URL and the the request will get the HTTP request will get a new method in this version which we can use to grab the path value so path value is a new function that Returns the name that we specified here so let's go ahead and say and we want the value of name and if you look at it it will always return a string so it will return a empty value if it isn't present so once we have this let's go ahead and save it and it looks a lot better right and you might be thinking well you're not validating right this returns a string so if we skip the name you will get an empty response right go ahead let's jump back restart the go program I'm going to curve and we will see that it works let's go ahead and remove the name and you will see that we're actually getting a 404 because this isn't a you need to provide at least one character but that's really amazing um that's a big Improvement to the HTTP router and these paths are just going to be amazing and going to help us a lot one other thing that we will look at is when you do like this what if what if we want to curl SL hello Percy and then have a another part of the route or a dynamic path so you might need a dynamic path for whatever reason let's say say after you want to do uh buy guess for fun and you're going to see we're getting a 404 not found because we're not hosting a route with that but what what if you don't know how many sections your url will contain due to various reasons well there's actually a fix for that if you want a dynamically long URL you can add three dots so to the last parameter you add three dots and if you do that you can go ahead rerun the API and now you will see that we're actually accepting more values which is great um also notice that the values following the name is actually part of the value that path value function returns back so this will return Percy and everything that follows the Slash that's not everything it's the the router is getting even better there's more we can do exact pattern matches by using a trailing slash the HTTP MOX matches any route that has the correct prefix what this mean is that if we remove this part if we do this if we do a/ hello route and we go here and we go back to our terminal and restart the program and and and you will see that we're actually getting a 404 not found but if we go back to this URL and add a slash in the end we're actually telling it to match any route starting with this prefix there's a different this is an exact match and this is a prefix match so if you go ahead and save that that jump back to the terminal restart the program go back to curl and you will see that it now works but there's very small difference but you should know it because I didn't and I've probably done this mistake not knowing it so there's actually a better approach to handle this which is added and we can do exact matches in the new router by doing the curly braces and adding a dollar sign so this will tell the router that this is an exact match so if we do this and this would be the same as the previous version of this it's just a little bit more concise so if we go back to the program and we rerun it we can now no longer curl that path we can curl oh my bad we can curl but we can curl slow and that's all we can match because we told the router to only accept exact matches uh now this is a little buggy because we're actually saying we want a path value but we're not specifying a path value so all right that's about it about the upgraded MOX the serve MOX uh but it's a it's a big Improvement now you might be wondering with all of these new features what will happen if we have conflicts and we can actually give it a go we can do a Hello so we have a route like this which will just simply print low right we also have a similar route but it's a little bit longer so we have the name variable here and let's do a fum Sprint we want if you specify any name we want to print it if you don't we don't want to print it so we can do below and the name the string and let's just do r. path value the new request function and we want to grab the value of name so let's make that let's make this so let's make this uh not only accept exact matches let's this route will now match anything right after hello with a prefix and this will match the name so how will it know well this one is more specific so it will select that by default so let's go ahead restart the go program in curl you should see the name but how did it know because because SLO and slow with the name variable since this is a prefix route it will actually match anything but this route is more detail it's more specific because we have more detailed in the URL it will always select the most specific route so that's about actually the most important changes that we will see um but they are pretty big I mean I think they will have a great impact in go more things are changing in the new version I don't feel that like they are worth covering in that so I will just mention them quickly you can actually check out the new random uh package with which is um m SL Rand SLV 2 and it has a pretty sweet new function called n let's do that so let's import that so the the first version to packaging go actually is the math random and N is a generic function so it actually allows you to insert a int64 for instance and then let's do this one it will return turn a random number between one Z and 10 and it's generic so that's cool you can even insert durations Etc uh for time durations that's cool so the slog package is getting a new function called set logger it's a pretty ugly name they didn't do well there it's set log logger level but this can be used to set the log level which actually wasn't so easy before so this is pretty sweet you can set the log level using this new set log logger level imagine if they could put more log in the name like set log logger level log I don't know they should have yeah I'm I'm thrilled because it will make slog even better so there's also get we're also getting a new set of slice functions so the slice package is actually getting a few new amazing functions so for instance there's the concat you you you start seeing that all of the new stuff that are arriving in go is actually almost always generic so that's pretty cool so what conab does is that it actually takes in a list of slices and concatenates Stu so like functions to help us work with slices all right these are the changes I think that has been worth mentioning the last part I kind of rushed through the changes because I feel that they're not worth spending a lot of time I think it's better to just kind of start using them I just want to mention them so you guys know that they exist and can start playing with them but they're so small I'm not going to put up code examples for them and I do hope you are as thrilled as I am regarding the changes to the HTTP router mostly and I'm super thrilled about that and I'm also super happy that the for Loop is freaking gone finally now with the new HTTP router I just can't can't wait to start building a HTM X app where I can try it out and just see what I can do with the new router and if you haven't already make sure again to update to the new version of go so thank you for listening and please let me know what changes are you most excited about what is your most missed feature in go just let me know in the comments and I do hope you enjoyed this video thank you for watching and have a great time with the new Go [Music] version
Info
Channel: ProgrammingPercy
Views: 9,636
Rating: undefined out of 5
Keywords: go, method routing, version 1.22, v1.22, go update, go changelog, go patch
Id: CXJ6Co2YBrM
Channel Id: undefined
Length: 27min 57sec (1677 seconds)
Published: Wed Feb 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.