Golang 1.21: 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 the changes in go 1.21 I know this is not corrent news but I still want to cover it Go 1.21 was released on August 8th 2023 and actually at the time of this recording go 1.21.1 is already available as well so there are packages for all the platforms and for all the different ways to install it I typically use home R but you can use uh whatever you want depending on the platform the docker images are also available for w 1.21.1 as well so what are the new features that I want to share with you there are seven features I want to call out let's start with the most popular one the addition of the slug package to the standard Library I covered the experimental version of that package in a previous video I'm going to be leaving the link in the description to that video so you can check it out practically the code that I show before is equivalent to the version when you use the standard Library the second new cool feature is the support for go tool change what this means in practice is that now starting with 1.21 you will be able to pull run times preview to the version that you're using when you're compiling a binary depending on the go module of the package that you happen to be using for building that binary and also is applicable to the Future versions what this means now is that if you happen to have 1.21 Z you should be able to compile any version that the specific dependency explicit in the case this is cool because you don't have to now go and explicitly install a Go version for building a package or for building a binary that you happen to need the next feature I want to share with you is go thebug this is a new directive that is equivalent to the existing go the buug environment variable that you can use to indicate a different behavior when you're running your go binary let me show you an example that I use for go 1.20 that is applicable to the tar package that indicates or rather prevents some security conc concerns when extracting files let me show you the code I have my main.go and there's an important directive right here what this means is that the god the buug variable if it doesn't exist or if it's not passed in during the run time of the of this binary it will say I want to overwrite the previous behavior and I want to start using the new Behavior and the new Behavior means I want to fail if there is an a non-secure file that's trying to write an existing file so if we close this and then go we build it or binary and then we run it you will notice that it's printing out this error and it's printing out that error because the file itself that I said before is trying to overwrite that value but what if we don't want that behavior we really want to keep things compatible with what we had before well we have to go and change the directive and instead of using zero we use one and we recompile this and we run it again and now you will notice that it will not fail it will print out the unsecure file now these are behavior that probably you want to keep because you want to build secure applications but in this case maybe there's some logic that changes and you need to still continue supporting the previous implementation one thing to keep in mind with this directive is that go debug environment variable still has the higher priority so if I go back and I and I will override it so it uses the implementation that I had before it will still go and honor what I had in the environment variable the next feature is the addition of three built-in functions mean Max and clear let me show you mean and Max are where you probably are imagining mean Returns the minimum value of the values that you pass in it's applicable only to order types Max is similar it uses the maximum again for only order types now the important bit that comes next will be using clear clear applies to slices and clear applies to maps for example I have a slice that is uh of two values hello and World it will print out what you expect to print out but if you call clear it would actually make the values empty it will not remove the values but rather make them the empty zero value when you use Clear is a slightly different and let me show you what I mean so if I compile this and if I run it you will notice that now the map it doesn't have the values compared to the slices if you notice the values were is still there the allocation of those slices was still there but in the case of the maps they are literally removed from the map they're still pointing to the same space in memory and similarly with the slices as well this this is an important thing to keep in mind when using the mean Max and well in this case the clear buil-in function the next feature is a new package called slices that includes some common operations applicable to slices let me show you the example now in this code I'm not using all the different functions that are available but I want to show you a few of them so index Returns the index of the value that you pass in so in this case the index of one will be minus one because it's not found the index of of two will be one because it's right here 0o one now there is something similar for sort that sorts the slice that you pass in as you can see the Sorting is in place it doesn't return a new value another function is the mean and Max and similar to what I showed you before with the build-in functions they do something more or less equivalent uh it Returns the mean mean Returns the mean value of the slice Max return the max value of the slice so there are kind of kind of the same these are not the only functions that you have in the package like I said before there are more I highly encourage you to look at the functions because most likely you have something that can be useful for your different use cases the next feature I want to show you is the addition of a new maps package that applies to the map type that includes common functions let me show you code so in this example I'm not using all the different functions available but there are some of them that I think are worth calling out so we have cloning we have equal we can actually now start doing a copy of the values and another one that I think work all out is this delete function that you can specify how to delete values in your map according to a function that you can pass in if we compile let me compile this I show you so we have the cloned is using different values and what I mean is this value this pointer is poed is obviously created two different maps and the other one will be like the delete that I show you before it's using anything that is higher than 90 it will be gone so I have Mario 91 it will be gone in this example similar to the SLI package there are more functions that I highly encourage you to check out on your own let's look at the next feature the next feature is addition of new functions to the context package there are four new functions added three of which will allow you to start your context interaction in different way let me show you an example that I have right here let's start with without cancel this is a new function that was added that as you may imagine it will return a context with no cancellation notice that although I have a timeout here the context without cancel will not return the error so when this is executed it will not print out that the time out happen this is an example where probably you don't want to propagate up what happened with a previous context you want to just a stop it right there next one will be with deadline cost this is similar to the one that already existed before with deadline the important bit to call out here is that you can indicate the error that will be triggered when the context is completed so for example you can say that the deadline will fail with this error not with any other error the previous context indicated this means means that when you're calling in CA it will actually print out this error that I have right here which is different to the error that you have in the deadline now what is the use case of this one well you can use this to explicitly indicate what cause the issue therefore ignoring the original reason it failed this could be useful when dealing with third party apis where you explicitly indicate the call fail instead of indicating the concrete reason another one that I want to call out is context after Funk this is an interesting one now because now you can ping a function to call when the context is completed so in this case I have my after Funk function that I'm passing in and I'm saying just print out cold and actually close the channel so what is going to be happening is that I do a timeout it will wait for 10 seconds it will complete and it will close the channel when the timeout expires and it will just say Okay deadline complete now this is a use case that probably it could be useful to trigger a new workflow after a context was cancelled without affecting the original context cancellation workflow something async for example now let's look at the next feature I want to share with you this new feature includes addition of three new functions to the sync package this is to lazily initialize a value on First Once similar to the way squence works and once value there's another function and on values as well let me show you the code so it makes more sense so what is going to happen here is that this once string is going to be called once once you call the function itself now this is quite interesting because it won't be called unless you call the function but it will be initialize once now there is another one called once Funk and once Funk uses a function instead of a value both of them are kind of similar so let me show you actually how this works in real life you notice right here that these are for one's value if we look at the code you will notice that I have a sleep here so I'm only called once therefore I'm Only Sleeping once so if you look at the time it waits you will notice that it will called was called once similarly with a computed once function we look go back to the code you will see that it's sleeping once but it's called once the confutation is completed it won't sleep anymore this is a nice way to probably compute values that are expensive but you need those values multiple times and that's it those are the features I think are worth calling out in go 1.21 of course there are more features and I encourage you to look at the release notes I'm pretty sure you will find something that matches any of your existing use cases thank you for watching I will talk to you next time stay safe keep it up and don't give up see you
Info
Channel: Mario Carrion
Views: 4,112
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 1.21, golang 1.21 context, golang 1.21 alpine, golang 1.21 tutorial, go 1.21, golang 1.21 changes, go lang 1.21, golang 1.21 slices, golang 1.21 slog, golang slog, golang toolchain, golang 1.21 toolchain, golang 1.21 sync
Id: fO2a-5Wyh0I
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Mon Oct 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.