From the course: Treating Go as an Object-Oriented Language
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Solution: OOP - Go Tutorial
From the course: Treating Go as an Object-Oriented Language
Solution: OOP
(upbeat music) - Now it's time for my solution to this challenge exercise. The first thing that I did here on line six was import math. And we're going to use that to calculate both the perimeter and the area of our circle. Now, in order to do that on the circle struct we need to have a radius of float 32. And on the rectangle struct we need length and width, both of which are also float 32. So now down here on line 28 we implement the new circle method. And this method returns a shape. So when you first typed this most likely you saw a compile error, because if you return a circle where you set the radius equal to the method perimeter radius, it would've complained that circle did not match the interface for shape because we had not yet implemented get area or get perimeter. So to solve that, I went ahead and implemented those. Now, I did a simple cheat here a little bit on get area. I used the built-in math.pi, which…
Contents
-
-
-
-
(Locked)
Structs as classes2m 21s
-
(Locked)
Creating a struct5m 16s
-
(Locked)
Encapsulation2m 43s
-
(Locked)
Leveraging encapsulation5m 35s
-
(Locked)
Solution: Struct as class2m 2s
-
(Locked)
Composition1m 36s
-
(Locked)
Leveraging composition4m 11s
-
(Locked)
Polymorphism2m 59s
-
(Locked)
Leveraging polymorphism4m 49s
-
(Locked)
Solution: OOP4m 56s
-
(Locked)
-