GoLang Structs - Custom Types | Beginners Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody welcome back to nerd academy i'm your host james and in this video we're going to go over structs and go lane so let's go [Music] the structs are used to group together values into one single entity a user-defined data type if you will this allows you to copy as a unit or pass to methods return to from methods or put in arrays much more easier so for example let's uh let's think of let's say an employee so it will have an id a first name a last name maybe a salary a position that would be really annoying if you have to go in and you're creating a method and you have to pass all those values to it it's much easier to create an employee's direct withhold field so it's just one entity that you pass through the method or return from the method so structs allow us to make our own types and define custom behaviors just like all the built-in types do so let's go take a look back in our trusty code editor of choice okay my linux obs kind of messed up so we're on the mac today as usual we got our package main and our funk main let's define a very simple struct so to make a struct we have to do type and then the name of your structs so let's do something like rectangle that's fun the keyword struct and then curly braces now within our curly braces we can define all of the values and each of these values we declare in the struct is known as a field so a rectangle probably has a length and that can be of type integer so this is a field it's a name and a type so it also has a width also let's do an int so that's basically how you define a struct pretty easy right now let's initialize one and there's quite a few different ways so let's go over each little one so the basic one is keyword variable and the name you want to name your struct so we'll just call this rect1 and we'll define the type it's going to be and now we can access each of those fields with the dot operator so dot length and then we can set that to i don't know five correct one width we'll set that to four another way is up i'm gonna call this one rect two rectangle with curly braces so this this is actually pretty much the same as this so we can also go in and wreck two length we'll set this one to six back to width equals i don't know let's say nine okay that's two ways there's a few more so wreck three same as kind of before but we can actually initialize the values at the same time so we can just go and go five and four so the order here matters so let's just say there was a bunch more different fields here and uh the order does matter so five being the first one which is length so it's in order of how you declare your struct so this will be length and this will be width just so we could see uh rect three length and width if i could spell three dot length and rect three top width i forgot one more value there we go now the next way is kind of the same because in rank four so rectangle and if you see my other videos this is kind of the way i usually do it so we can actually say width first and then length order doesn't matter because we're we're specifying which ones we want and then the last two as a beginner you probably won't use but these are pointers and references so kind of do what we did before rectangle five keyword new as you can see it's going to return a pointer so this is a new rectangle and just like before we can still access all the fields with the dot operator and if we don't set any of the other ones it's going to be zero or whatever like a string it's gonna be an empty string and an end will be zero and rectangle we'll just do rectangle six this is a reference it's pretty much the same thing so this is a pointer and we can do the same thing with the dot operator now we can also embed structs with instructs kind of like a struct exception huh i did this in my last video with the to do's and the page data so the page data was a struct and then in the page data was a struct uh my user-defined type of to-do's since you define a type it is a type that you can use in a struct just like an inch string or a float or anything like that so as an example we'll create a struct of a let's say person struct curly braces and let's say name string age unsigned int 8 because that's already more than a human lives and then anything else you want there and let's name let's do an employee like we talked about at the beginning employee's gonna have an id we'll do unsigned int 64 because who knows it'll be a lot of different many different employees let's say position up uh let's do person name person type so you can see this is a field person the name and then the type person which we just defined above and let's do salary ascendant and anything else that makes up an employee that you want go down to the bottom here and we'll create a great bob he'll be an employee and then we have an id we can set that to i don't know 78 34 8 4 5 yeah salary poor bob only makes 35 000 a year and then we can set the person to a person and then we define this person so his name is bob his age let's say he's 30. all right so as you can see we have an employee and then in within the ploy we have another type user defined type of person go ahead and print out bob and print out his whole type his whole structs just by passing bobbin and i haven't printed anything out so let's see what we got we got a bunch of different stuff we should look at so our rectangle three we set length and width before five and four so like i said the length is five the width is four and then rectangle four with four and length four and with five and then bob this is employee id bob is 30 that's his name and his age and then that's his salary and another neat thing is uh using your structs for uh marshalling json so these are used with something called field tags and it's the only thing i've ever seen them used for if you've ever seen anything else for these field tags leave a comment down below because this is all about learning and i learned something new all the time so let's go up here and it's the backticks and then we go key is json and the value we set here is what the json is going to encode and what you can read on the other side it's like an alternate alternate name for what we're going to use here is name so instead of uppercase name it'll be lowercase name that you can access from a json let's make a person call this one susan person name is susan age is 24. and to make the json object we uh there's a method called json.marshall and we pass in there is an interface for us that's susan but marshall returns a array a slice bytes and an error so we should capture that so we'll just call it data and error set that equal to the json marshall and this is i'm going to make a video about error handling but kind of got to do this you got to check if it's not nil and if it's not nil there's an error so we'll just go ahead and print out error i'm just going to print out the string air dot error this is just a string of what the error is you can make your own custom error types too now we could just print print out the json all right let's run that and see what we get and we get byte data as we expected so if you actually want to see the string we're going to format print f percent s i did value which is which is the byte array this is going to convert it to a string see if this works now all right there we go so if you can see our json string has a lowercase name and the value is susan for the field and age is capitalized because we did not create a field tag for this field so very useful and finally i did mention we can create our own behaviors on our data so we what we do is there's two different types there's a value receiver and a pointer receiver we'll get into that in a second we'll just do a value receiver first so it's funk and then in parentheses we're gonna name the variable and then it's going to be a type so let's say we're going to get area on a rectangle so in our method here we can call we're going to probably have to return an ant here so we also need a return type so how do we get an area of rectangle it's length times width so return r dot so if we define if we initialize a struct of rectangle type we can call this method on it so let's go let's call um on our first rectangle area of rect one so rect one dot as you can see we got get area available there you have rect one is twenty five times four is twenty confirmed all right so i did mention a pointer receiver and a value receiver now this one we did was a value receiver which essentially let's do something real quick let's say we want to just for example sake set length i mean you don't have to do it this way but so we're going to set the length we're going to bring in this new length and then the the rectangle we call this upon we're going to set it with r dot length equals length i mean you don't have to do this way because you just access it but for example sake we're going to use this method so the difference between a value receiver and a pointer receiver is changing the value so let's change the value of the structured column of spawn so let's do this set length otherwise let's just comment pretty much everything else out for now so let's say rect1 dot set length let's set the length from five to seven this out new length let's see run that no see we set it to seven but the same is still five so basically a copy has a copy it doesn't have access to the original that you're acting upon so right here when we set length we're copying rect one over to this method it sets the length and that's it we don't get the other copy back we can return it but there's an easier way it's called a pointer receiver if you don't know anything about pointers it's kind of a difficult subject to wrap your head around if you're just beginning with uh with programming basically it's a pointer to something so it points to something so it's an address this would be copying by what's known as reference when we do this now with the pointer this variable rect1 in your memory has an address somewhere on your ram stick let's just say when we call it by copy by value it just copies it and it doesn't have access to the address it's living at when we pass the value by reference the method has access to the actual the address so when it updates a field of your data type it will actually reflect in your data so you don't believe me we'll we'll do this right here so remember our length was five we're going to set it now to seven actually let's get the area after after two we'll go ahead and run this again so now the length is seven and our error the rectangle after we updated the field by by a pointer receiver so basic rule of thumb on using the difference between these two the value receiver and the pointer receiver use the value receiver which is copying by value if you do not need to update any of the fields in your in your data type but if you do need to update any of the fields in your data type you should use uh the pointer receiver hopefully that wasn't too confusing i plan on making a video about that sometime in the future so stay tuned for that and as usual all this code will be available in the link in the description below and if you want to see more about golang just watch this video over here and we'll see [Music] [Music] you
Info
Channel: NerdCademy
Views: 176
Rating: undefined out of 5
Keywords: software development, software development tutorial, programming tutorial, golang tutorial, go tutorial, go programming, golang programming, learn go, learn golang, introduction to go, introduction to golang, golang structs, go structs, structs in go, structs in golang, golang structs tutorial, go structs tutorial, value receiver golang, pointer receiver golang, golang structs for beginners, golang classes, go classes
Id: WsciIWP-8DQ
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Thu Sep 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.