Last active
November 5, 2019 19:53
-
-
Save aric49/a2619563ff3a88629b9a5410efadbb74 to your computer and use it in GitHub Desktop.
Object Oriented Go
This file contains hidden or 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" | |
| "strconv" | |
| ) | |
| //let's define a struct | |
| type car struct { | |
| color string | |
| model string | |
| topMPH int | |
| } | |
| // Let's define a method | |
| //define the local variable "mycar" and tie to the 'car' struct using *car | |
| func (mycar *car) printCars() string { | |
| var myString string = "my car is a " + mycar.color + " " + mycar.model + " and it goes " + strconv.Itoa(mycar.topMPH) + "MPH!" | |
| return myString | |
| } | |
| func main () { | |
| //let's define some data | |
| car1 := car{color: "blue", model: "chevy", topMPH: 60} | |
| fmt.Println("This is the first car: ", car1) | |
| fmt.Println("Here are the fields: ", car1.color, car1.model, car1.topMPH) | |
| car2 := car{color: "green", model: "Ford", topMPH: 80} | |
| fmt.Println("This is the second car: ", car2) | |
| fmt.Println("Here are the fields: ", car2.color, car2.model, car2.topMPH) | |
| //Call the method using the dot operator: | |
| fmt.Println(car2.printCars()) | |
| fmt.Println(car1.printCars()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment