Golang Tutorial #20 - Structs and Custom Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back to the golang tutorial so in this video I'm gonna be talking about strux and how we can create our own custom types in go line now if you haven't seen another programming language before then you may not be familiar with the notion of classes but inside of other programming languages you can pretty much write some syntax like class Tim and then inside of this class you define a bunch of methods you define a bunch of attributes which again you might not know what they are that's totally fine and then Tim it becomes actually a custom object or a custom type that we could use so to really illustrate this when we make a variable write every variable is some type and we've gone over there's so many times it's really important in golang but if I say something like you know var X in equals 5 of course this variable X is an int and the fact that it's an int gives it some very specific behavior I can add it to other ends I can subtract it against other ends I can use it in specific contexts and if it was a float then it would behave a little bit differently right it has a different amount of precision it takes up a different amount of space all these types have their own kind of like features or different things that are specific to that type right and of course we had a raise which acted a little bit differently we had maps each type has its own kind of custom behavior and that's what I want you to really think about is that these types here all have their custom behavior and what we can actually do is make our own types that have their own custom behavior and the reason we would do that is because sometimes the current types that are given to us are not enough we actually want to combine and use those together in our own type that makes it more valuable more readable just easier to use in our program and hopefully I can show that to you here so what I'm gonna do is just make a struct that stands for a structure this is just your own custom type and all we go through this it'll make a lot of sense but there's a lot to explain so it's kind of incremental we're gonna understand so I'm gonna make a type called point I'm gonna put type point struct and this is how we set up our own custom structs or types so we write type we put the name of the type always we start with a capital letter that's pretty much a convention you always do that and then you write struct struck defines that this is a custom type because there's other things that we can do with type we can actually do type I and then we can write something like interface now I'm not gonna show that right now but in a future video so just understand that we're saying this is a structure which really means it's just a custom type that we can use okay so type point struct and then inside of here what I actually do is I list the fields that I want to be a part of this struct or of this custom type so before when we had types we only had one type if that makes sense we had like var X int X is just an inch right but now what we're gonna be typing is like var X point is equal to and we're gonna put point like this and we're gonna define what makes up a point so inside of here I'm gonna put two fields that will define what makes up a point so I'm gonna say X int and then Y int like that and we'll make them both in 32s so what this is saying is that a point is its own custom type and it has a field X and a field Y so this point will store some x-value and y-value for every single point that we make so if I do something like var X point equals point one two what this is saying is let's make a new point that has X equal to 1 and y equal to 2 and obviously you know point I'm just talking about like a two dimensional point on some graph so we have an XY value and of course what I can do as well is I can actually say let's let's change these values so I'm gonna say P one and P two point one and point two and we'll make a new point and maybe this point will be equal to like negative five and seven so now we have two points one of them has an x value on a y-value of 2 one of them has an x value negative 5 and a y-value of 7 so you can see where this might be useful right if we have kind of objects that we want to represent that we can't just represent with one INT value or one map or one array we make our own custom type we've called this a point and now we have point objects right that have an X and a Y value now let me show you what these actually look like when we print them out and how we can actually have a look at on some of the specific attributes of them so if I go fmt dot print line what can do is since you have this field X and this field Y inside of your point type you can access those fields directly so I can say something like p1 dot x equals well no I'm not gonna say equals sorry because I'm inside of FM t but if I print out p1 dot X and let's have a look here and I will actually have to print out or comment this out for now because we're not using it okay so let's print out p1 dot X and we see we get the value of one right showing up for us there because obviously the x value of this point is 1 because when we pass it in and assigned X equal to 1 y equal to 2 because that's the order that we pass them in now if I change this to Y you guys can probably guess what we're gonna get let's have a look here and we get two pretty straightforward now here's a cool thing though to have a look at is that some people oftentimes get confused and they think that this x and y can only ever store one value so they think when i make this new point p to that point one's values will change right they won't be one and two that is incorrect we can have a bunch of different point objects that all have their own unique x and y values so look here if I print p1 dot y and then I print P 2 dot Y right there you'll see that these are two different values and nothing changes like they both can store their own unique values so we have two and seven right so these point objects are just both different point objects that each have their own x and y values so that's really useful and that makes it a lot easier to do certain things in program so I can actually change the value of p1 dot X I can change the value of P 1 del Y as well by saying something like p1 x equals and let's change it to 7 right so if I go ahead and run this there won't be anything wrong with the program oh well because I didn't use p2 but that does actually go ahead and change the value of one here to 7 you can directly change the fields now these fields don't need to be the same type in fact what I can do is say like XY and then I can say is on grid or something and make that a bool right so now we have another field called is on grid and that's a boolean value and now when I make my point I need to pass false in there right or I need to pass true another value so that we set something for is on grid that is the idea behind strux and how you make your own custom points because notice these are type point right so we're making our own types this is something that is a little bit difficult sometimes to wrap your head around but once you get it it makes things a lot easier so we have point that's good I'm gonna get rid of the is on grid because I don't actually want to use that and what I'm gonna show you now is actually how we can make types that use other types so point is a type now it is treated just like in 32 float32 whatever there's a few small differences but you can think of it like that right because I can just write type point no problem whatsoever now what I'll show you as well is that I can define a point using a few different a few different ways to define a point I don't need to write it like I just did here the most common way is to do something like p1 colon equals point 1 2 now this will just automatically infer that this is gonna be type point there'll be no problem with that but let's say then I want to make a point but I actually don't know what the y-value of my point is yet I just want to set the X I don't want to set anything for the Y value well what I can do is I can actually say point X colon I can set a value so I can say X colon 3 like that and that is totally valid and fine that's just gonna mean that Y will be set equal to whatever the default in 32 value is which is going to be a 0 and I'll show you if I fmt dot print line here p1 then that's what ends up happening so let's have a look down here and we get 3 0 so X is 3 y is 0 because I set X but I didn't set what now I could do the other thing too I could put Y like that and now if we go ahead and print it we'll see it swaps around so we get 0 3 there we go 0 3 no problem so that's another way to define points I believe that's known as a literal now what I want to show you though is that when we start using these kind of structures this is where pointers become very important and I'm gonna talk more about that in the next video when we get into methods but I want to show you what happens when I put an sign like this because it's something very common that you'll see I'm saying p1 is actually equal to the pointer to this point now I know that seems like a weird thing to say but let's run this and have a look at what p1 is now and let's see and we get the an scene for this value so what that's telling us essentially is that this is a pointer p1 is a pointer to the point that has the values XY right inside of it so it has those two specific values now what this means is that if I pass this pointer around to like another function or something I can modify the value of this point directly so let me just show you because we showed this in the pointer 1 so I'm kind of building on here I'm gonna say change X okay and let's say that we're gonna take some points so we'll say PT point is what we want to take we're not gonna return anything and we'll just do that so now again since this is a type I can put points up here no problem because we've defined it and what I'm gonna do inside of here is I'm just gonna say and story this should actually be an asterisk I'm gonna say PT dot x equals 100 ok and let's have a look at what I want to know okay so let's say p1 like that and let's pass that to change X let's go to change X t1 and then let's actually fmt dot print line p1 before and after just so we can see what this looks like okay so what I'm doing right now and let's just walk through this I'll slow down for a second here is I'm taking some point that I've created so I've made a p1 it's equal to the pointer to the point that is defined by this right so before if I didn't have the pointer I'm saying this is equal to this point object so it's the value right it's not equal to the address it's equal to the value of this point I can still pass it around I can still use it but now I've said it's equal to the pointer to this point which means that I can actually have the reference so I can change things inside of like a function right which we talked about in previous videos so I'll start by printing out p1 then I'll call change X on p1 and then I'll print out p1 again and we'll see if it actually changes the value of x and then I'll show you a contrast for one name does or doesn't so it does the opposite of what we just did so let's have a look at this here go run tutorial echo and let's see what we get okay so we get 0 3 103 so clearly it did change it although we didn't change PTX and and here it did actually end up changing it to the value 100 and you can see that these are both still pointers right now what happens if I do this so let's get rid of the pointer here and let's get rid of that and now let's see if any of this changes so I'm no longer gonna pass a pointer when I pass that to change X I'm actually going to pass the value of p1 so I'm just gonna pass that object let's have a look at what happens now we get 0 3 0 3 so we actually didn't end up changing the value inside of the point now that's weird why did that happen well when we don't pass the pointer of one of these struck to a function what ends up happening is we actually just make a copy of this point so we say ok we're gonna pass the value of point which is you know X is equal to 0 Y is equal to 3 we pass that to change X it kind of makes like a copy of that and then it changes its own copies value to be X so if I go down here and I say fmt dot print line PT you'll see that here inside of this function yes this PT x value has changed but down here it doesn't get changed because we just passed the value we didn't pass the pointer and I hope this makes sense cuz we talked about this a lot in the last video but I just wanted to show you that that's why I went into that talking about pointers right because they're very important to understand so if we have a look here we go go run tutorial go we see that we get 0 3 103 which is this print statement so this actually did change the point that was here but down here it didn't change it so just keep that in mind there's some cases where you don't want to pass a pointer you'd rather just have this object make a copy right or you'd have this function make a copy but in a lot of situations you do want to pass the pointer and when you're passing the pointer you do the same thing where you put an Asterix here to define you're getting a pointer not the actual value and then you're good to go now some of you may have noticed I'm gonna delete change X and get out of this example a little bit here that when I had passed the pointer to that function I actually didn't need to dereference it before I change the value of x so there's this cool thing on Struck's that lets you do this so if I have p1 equals a pointer to the point you would think that what I would have to do is say asterisk p 1 x equals like 8 or something writer I would actually have to do this Asterix P 1 dot X so I would say let's get the actual value right and then let's change it so I'm dereferencing the pointer but the way that structs work is it doesn't matter if you have the pointer or if you have the value you can directly use this so you can directly use the dot operator to access any of the fields inside of your struct so X or Y again I know this is getting like pretty confusing but this is something that we're gonna keep going through so it'll make more sense to at the tutorial series but I just want you to know that it doesn't matter if your what is it if your struct is a pointer or the value so if we have the ampersand vs no ampersand it doesn't make a difference we can still use this dot operator to access any of the fields inside of the struct now the last thing I'm gonna show you is an embedded struck now this might be going a little bit far for some of you guys but I do need to cover it as is an important part of go length so I'm gonna show you to say type circle is its own struct and inside of this struct what I actually want to have is a point and a radius so I'm gonna start by defining the radius so I'm gonna say radius is a float64 and I'm gonna say that the point is actually type point now what this is pretty much saying is that we actually have a field inside of our circle that points to another struct and in fact I'm actually going to put an asterisk here to define that this is a pointer to the point because when we have a circle I want to have some point where the middle of the circle is so we can actually call this like the origin if we want it or we can call this like Center let's just do that and that's equal to a point so we're gonna use a struct inside of another struct so how would I actually go about doing that well there's multiple ways I can do it so first of all I can define a point like this with the ampersand so I'm saying this is a pointer to this point and then what I can make a circle so I could say c1 colon equals let's say circle and then inside here we need a radius so let's past like four point five six four point five six and then let's pass p1 as our pointer right or as our point so we're saying that we have four point five six which is the radius and then since the center is a pointer to eight points we simply pass the pointer to the point that we defined here which is p1 now I don't have to do that I could also do this I could write the ampersand points because again we want the pointer not the value and then I could put like four or five in here and now I've set a hub have a circle with radius four point five six and then I have a point inside of the circle with the value four or five now this is where it gets interesting how do I access X Y from this point well let's have a look at how we can do this here so I'm gonna delete p1 and I'm gonna go and then they start by just FM t dot print line c1 and I want to show you what this looks like so let's go go run tutorial dot go and let's have a look at this okay so four point five six and then we have some pointer notice that's what that's printing out right so now let me show you what happens when I print out c1 dot center so c1 Center gives me let's have a look at here the actual point right here so it's saying it is a pointer but it's giving me four or five so if I wanted to access the x value of my points what I would do is do c1 dot center dot X and once that's what that is saying is I want to access the X attribute from the center which is that point right in there ego I'm able to access it so that's how you access an embedded struct and same thing if you wanted to change something on the embedded struck you do see one dot center dot Y or Center X now it's important that usually when you have an embedded struct you use the pointer to it not just a struct itself um we can talk about that more as we get into methods in the next video but what you can also do and there's are so many things with structs I'm just trying to show you guys as much as I can because you don't have to actually give it a name if you want to have an embedded struct inside of another struct you can just write that struct like this so if I just write this like Asterix point you can see that I'm not getting any errors up here here I am because I'm referencing Center but what this says now is if you just put point um it will just as long as none of the attributes from here are the same as what's inside of here you can just do this now you can do C 1x like that and that will actually reference the X field from the point so since I'm feeding in a point here and there's no attributes of the same name from point and circle I can reference x and y from the point from inside circle without having to call some intermediate name like Center so I can just do C 1 dot X right so IOC 1 dot X I go go run tutorial go we get for no problem and I can do C 1 dot y as well and then of course I could reference C 1 radius so that's kind of how this works when you do an embedded struct you can just put one directly inside or you can give it a name and then if you give it a name you use the name and then the dot operator for whatever attribute it is you want to reference okay so I feel like that was a lot I totally understand that this might be a little bit confusing to some of you guys but again I just need to go through this this is now where we're getting into some of the more complicated features of go line in the next video we're gonna talk about methods which are actually really important and that will hopefully shine some light on a little bit more how these kind of structs and types actually work so with that being said I hope you guys enjoyed if you did make sure you leave a like subscribe and I will see you in the next YouTube video
Info
Channel: Tech With Tim
Views: 22,395
Rating: undefined out of 5
Keywords: tech with tim, golang structs, structs golang, go structs, golang types, go programming language, go programming tutorial, golang tutorial, golang
Id: dm9oXt6_YNA
Channel Id: undefined
Length: 18min 49sec (1129 seconds)
Published: Thu Jun 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.