Created
October 29, 2019 21:18
-
-
Save TheMorgz/5f103a2a64bd965e01ecac780ecb0ab8 to your computer and use it in GitHub Desktop.
Golang Exercise to practice structs, interfaces, and methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type cube struct { | |
length float64 | |
} | |
type box struct { | |
length float64 | |
width float64 | |
height float64 | |
} | |
type sphere struct { | |
radius float64 | |
} | |
type ofStructure interface { | |
volume() float64 | |
} | |
func (c cube) volume() float64 { | |
return c.length * c.length * c.length | |
} | |
func (s sphere) volume() float64 { | |
return (4 * math.Pi * math.Pow(s.radius, float64(3))) / 3 | |
} | |
func (b box) volume() float64 { | |
return b.length * b.width * b.height | |
} | |
func calculateVolume(kind ofStructure, called string) { | |
fmt.Printf("The Volume calculated for our %s is: %f \n", called, kind.volume()) | |
} | |
func main() { | |
c := cube{ | |
length: 7, | |
} | |
b := box{ | |
length: 5.5, | |
width: 5.5, | |
height: 7.7, | |
} | |
s := sphere{ | |
radius: 7.14, | |
} | |
calculateVolume(c, "Cube") | |
calculateVolume(b, "Box") | |
calculateVolume(s, "Sphere") | |
} |
Hey, I'm glad I could assist you with your journey🙏🏾
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey thank you for sharing this. Was having difficulty understanding interfaces