Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Basic Struct Exercise" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to create a Circle struct with a single field radius of type float64, define a circumference method, and, as a bonus, add a method for calculating the area. The exercise solution can be found on the live_workshop branch linked below.

Preview
Close

Transcript from the "Basic Struct Exercise" Lesson

[00:00:00]
>> With all that being said, I did prepare an exercise. I think everyone here will actually just eat that exercise like nothing. I think you all will be able to do it in one blink. I'm still gonna show it, so yeah, this is gonna be kinda the last thing we do for this portion of Go.

[00:00:18]
I'm just gonna create something, you don't have to copy this. I'm just gonna paste the exercise in here. It's gonna be just exercise at text. And then I'm gonna go to my secret screen here. All I wanted you to do was create a circle struc with a single field of radius, type float 64, and then add two methods onto it.

[00:00:40]
One method is calculating circumference and one method is calculating the area. I have the formula right there. You can use pi as a constant. That's actually, hold on, I didn't go through constants. Super quick, it's gonna make a constant, it's just const. You can do, let's say pi, you can make that 3.14.

[00:01:04]
So this is constants. Typically, they're declared at the top of your package of your file, cuz they're treated as a global variable, and Go will not complain about unused constants. Cuz it's const, it doesn't have to worry about any kind of memory management, memory allocating. So it's just like, okay, I know what you are, I can infer your type, go rate your code.

[00:01:24]
So that's why you can use a const, okay? So yeah, basically all this is pretty straightforward stuff. You'll have your package main, I don't even think I need to do this part. Make your package main, add some imports, create your circle struck, add those two methods, and at the end, have a function that just calls a circumference and prints the actual value and area.

[00:01:53]
Okay, let's just go in the main. Let's just delete all of this. Let's do const, pie, so yeah, make your const as 3.14, let's do a type, Circle, it's gonna be a struct. What I want, the radius, so capital or not, it's fine, you can make it whatever you like, this is fine, float.

[00:02:21]
Cool and it's gonna say unused and let's make a method on this, so func, I want to calculate the circumference so let's pointer to Circle, Circumference kinda a long title but not going to need any arguments here. Let's go here and just do, what's it? It's 2 pi r, all right, so we'll just say 2 pi.

[00:02:52]
So we have the constant pi area, we have radius. Okay, cool, so what we could do is just, let's see, circumference, I didn't wanna return it, actually, I want to print it. Fmt.print. Cool, and let's actually just create a quick helper function, func NewCircle, it's gonna take radius of float64, then all I want to do is return Circle, radius as radius, Then return type of our Circle.

[00:03:46]
And all the way down here, let's create our new circle. So myCircle is going to be a NewCircle, radius, I don't know, but raised to everyone use any values.
>> 4
>> I already put 1.5 [LAUGH]. That's it, this is basically how we do it. I don't think this needs an explanation.

[00:04:19]
Const, method, create a new circle, you don't even have to have this new circle function. I do it as a habit. Just makes it a little cleaner. Don't need it.
>> So Go seems to know that the calculate circumference method is on the circle type. Is there a way to add those methods into the type or is that not important?

[00:04:41]
>> Which method into the type?.
>> So you can call myCircle with calculator conference.
>> You can call it, yep, right here.
>> So is the calculator conference just kind of, it seems like it's extending myCircle by adding a method a little bit, but the type doesn't need to have the method on it.

[00:05:00]
You could just get keep adding methods to the type.
>> Yeah, so that's kinda the principle of the composite. You can keep composing your struc to include methods of different types and different structs. And this method, I don't know if I would use the word extend. It's just a method that's bound to the type.

[00:05:20]
And so circle being a struc, which is a type, it just bounds that method onto that type. And you can bound more methods onto specific types. You can actually bound methods on a different struc, and then your parent struc and have that struc embedded. And that will also have the method available to it.

[00:05:35]
So that's kind of how, it's a blocking piece of composition versus just like extending it, kind of limitlessly.
>> Okay, but can you add a function to a type?
>> Yeah, yeah, yeah, what do you mean, add a function?
>> I don't know could you, [LAUGH] could you type the circle and then have circumference on that type?

[00:05:56]
>> And then that will call the circumference. And that will call a calculus circumference, essentially. You can, I think actually, no, I think you have to create a method which is just, I don't think it's gonna need func type in a struct. I think that's where interfaces come into play and that will kind of open up the grounds for how you can have different functions on a pool of structs.

[00:06:21]
And they can call it c dot, circle.circumference or whatever.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now