10 secrets in Go (Golang) (5m Friday #4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome guys my name's Steve and today we're doing 5-minute party where I share my soft engineering experience with you so I was reading this interesting book I bought recently and I found a lot of interesting things I found a lot of interesting things about the universe you know what I didn't find anything about the universe I was just reading the book for myself but today are talking about ten secrets ago things I consider are important things that consider will improve your life cycle your lifestyle as a going-over so dr. ado let's go ahead and throw in five minutes on the clock so we're gonna begin our top 10 secrets can go with slices and specifically we're talking about the difference between an empty slice and an ill slice so that further ado let's go ahead and have a look at an example to show you exactly what I mean so as you can see from my example we have two types here we have the people type and the response type and then we try to marshal this JSON and we try to output this to the terminal so when you run this example the following lines aggregate printed and notice here you have items equals no here and you also have items equals null here and that is because we didn't specify that we want to return an empty slice we want the items field to be an empty slice so as you can see right here we just returned the response we returned an interest cost so basically in both cases you're gonna get to null as a result in the Jason so in order to avoid that you have to be explicit about the end result so you have to say items field equals to an empty people slice in this case you're gonna get an empty array as you can see right here alright so we continue our secrets and slices and next we're talking about something called slice free index or slice or index what exactly is that and why you should be aware of that so just like in the previous example we try to create a new slice called people and then after that we try to put a sub slice of that original size so we all know this syntax we know that the first index is the low and the second index is the high so basically it's gonna try to create a slice starting from index 1 after index 2 so as you can see here the first index is nothing else but max so basically the Pertinax is gonna determine the final capacity of this sub space and why exactly we should care about the final capacity because that is not so important just the capacity and that's it right so basically if we use only the first two indexes and we run this example as you can see something we're just gonna happen the capacity is not gonna be one but it's gonna be two and also as you can see here the second element of this new sub slice we create has the same address as the last element of the original array we're basically going to overwrite the last element of the original and we're also going to overwrite the last element of this new subspace and that's why it's important to keep that in mind it's important to limit the capacity of the new sub slice alright guys so the next thing we're talking about our Jason's truck tags what are those if you're new to go and what are the possibilities when it comes to using Jason's truck tags so as you can see in this example we have a custom type called person and we have a couple of strike fields we'd have named hobby email and money and we have a couple of Jason's truck text now when you run this example you should see something like the following so as you may already guessed if you're new to go a Jason's truck tag is basically a way to instruct the Jason encoder how to encode this specific struct when it comes to outputting it - I don't know - Jason so the simplest way of doing this is by having these back takes Jason : and inside the quotes having the name of the field which is gonna be encoded inside Jason now besides this you could also do something like that Jason hobby for example and having a comma and then having a medium so basically omit empty is gonna meet every field in the JSON output which doesn't have a specific value which has a zero value if you will so as you can see here in the second example we are not using the hub basically in the first example we are using the hobby in the second example we are not using the card and if you run this example as you can see in second line we don't even have the hobby field in the Jason out when it comes to numbers and conversion into string in the Jason struct axe there is a powerful feature called the string if you run this example again as you can see in all examples we have money and it's actually a string that's not the number so next we are talking about type aliases what exactly are those and how they differ from custom types so again let's go ahead and have a look at an example to show you what exactly I mean by that so as you can see here in this example have a custom type called human and we have a couple of methods attached to this type so this is how simple it is to create a new type alias basically you say type you give it a name you say equal and you specify an existing type you specify an already existing type and why is this powerful well because you don't have to basically recreate this type assign these methods to this type and replace it with students everywhere basically you don't have to do this crap and that's why I type aliases are usable so the next thing we're talking about on our top secrets is anonymous struct and empty trucks and why I think they are important why I think you should know about them as you can see in this example it's very easy to create an anonymous truck you just say struct you specify the fields and inside the curly braces you specify the values so basically if you don't want to create a custom type you would usually return an anonymous type and that is a very neat feature when it comes to simple data structures next let's have a look at an example about m2 structs and why they are so powerful why they are such useful features so as you can see in this example we have two go routines and these go routines try to write something to the channel so they write something to this child it's just write an empty struct and then we have a four select construction and inside here we'll listen for that dawn channel and basically if we had like millions of goroutines here it would be convenient because that empty struct doesn't take up any space and it would not consume a lot of data between these quarantines when they communicate with each other all right so the next thing we're talking about is concurrency and specifically we're talking about the goroutines order the order in which you write your code is not gonna be the order in which they are on the order in which they are spawned so as you can see in this example it's a very simple example first of all we try to set up the maximum number of threads and which go is going to run and then we try to spawn we try to run to go routines supposedly concurrently so as you can see every time I run this example 1 & 2 gets printed that's because we only have go routines on one thread and basically these go routines are gonna run one after the other however running our code on a single thread is not efficient and it's not likely to happen so let's go ahead and comment this line so basically when you execute this code now it prints 1 2 2 1 as you can see the word Affairs alright so the next thing we're talking about is concurrent safe types so let's go ahead and throw in an example and I'll show you what exactly I mean by that I'm gonna open up my terminal and make sure to run this code of the - race flag to make sure it doesn't have any databases as you can tell it doesn't have any data race so as you can see from this example we at least have two ways of breaking this code so basically we have two rights operations right here in the goroutines in these simulated HTTP requests and we have one read operation at the end when we try to attempt to read the result so let's go ahead and break in the write operation first so basically inside this function set great we're gonna try to comment out this metric when you run this code it's gonna warn you that you have data races and it's gonna give you some stuck trace and all that stuff so I'm gonna try to attempt to read the results right away without waiting for Google Keynes to finish their jobs and this time we're gonna have a data race when it comes to reading when it comes to the read operation so as you can see most of the built-in types are not concurrent saved that's why make sure when you run concurrent code you actually protect it from data race next thing we're talking about is shadowing almost everything so to prove you all exactly I mean by shadowing almost everything let me go ahead and show you an example of that so as you can see in this example we try to override everything we try to override the make function we try to override the append function try to override the JC and we try to override a lot of things we even try to override the boolean true and that is a variable now so every time we say a variable equals true it's gonna be false and that's weird and try to avoid keywords functions built-ins and all that stuff otherwise you're gonna shadow them and you're gonna have a hard time finding the bug all right so the next thing we're talking about our interface values and why they might be a slight source of bugs and why you should pay attention to them on writing goal code so as you can see from this example we'd have a small function called adult and we basically verify for this in so basically receiving in here and we verify that this if this is smaller than 18 we return nil otherwise we return exponents now notice here response or rest is a pointer to person and the value is nil so when you run this program you're gonna see that it's gonna say hey can you finally got a dog when in fact it should clean this line but it doesn't print this line because the value and the type are not new the value is new but the type is nothing the type is pointer to person all right guys so lastly we're talking about pointer versus volume receiver when it comes to type methods and creating methods of a specific type so as you can see in this example we have a type called people and basically it's a slice of string and we have a couple of methods we have a method called add and we have another method called add PTR so basically add is gonna use the value receiver and add Picard is gonna use the pointer receivers when you run this example as you can tell the first case which we use the value receiver is not gonna work in the second case when we try to append to this slice it's gonna work now notice here when we created the world variable we created the people type using an ampersand which means this is a pointer now even this is a pointer and we have a value receiver it's still not gonna work so even if it's a pointer it's not gonna work make sure whenever you want to update the internal state and that data structure you're working with is not a pointer itself like a struct make sure you add in an pointer receiver in order to update its internal data otherwise just use a value receiver alright guys that is pretty much it on this video and that is pretty much it on this five-minute Friday so hopefully this wasn't a long video and it was I'm super sorry for that so when it comes to the tenth secret sing go it was hard for me to pick which one of these made it into the final list so that's why if I miss something where I didn't cover something make sure to let me in the comment below also make sure to check out the video description below because in the resources section I linked up everything that I used in this tutorial several links to useful articles you may read about the topics which I covered and of course the source code and that's pretty much it on ten secret single guys I'll see you in the next video [Music]
Info
Channel: Steve Hook
Views: 20,174
Rating: undefined out of 5
Keywords: steevehook, stevehook, steve hook, golang, go, introduction to golang, go basics, software development, web development, software engineering, computer science, 10 secrets in go, 10 secrets in golang, golang tips, golang gotchas, slices in go, slice three index, go routines order, json tags, empty struct, anonymous struct, type aliases, concurrent safe types, nil interface{}, interface{}, pointer receiver, value receiver, 5m friday
Id: IB2JzpXaXo4
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Fri Mar 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.