Skip to content

Commit

Permalink
Structs
Browse files Browse the repository at this point in the history
  • Loading branch information
plasmus777 committed Apr 19, 2024
1 parent 0129d8e commit 8c10643
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions learning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
fmt.Println("11 - maps")
fmt.Println("12 - functions")
fmt.Println("13 - pointers")
fmt.Println("14 - structs")

fmt.Println("")
fmt.Println("-1 - exit")
Expand Down Expand Up @@ -78,6 +79,9 @@ func main() {
case 13:
simple.Pointers()

case 14:
simple.Structs()

case -1:
fmt.Println("Exiting program.")
return
Expand Down
49 changes: 49 additions & 0 deletions learning/simple/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package simple

import "fmt"

type Plate struct {
letters string
numbers string
}

type Car struct {
name string
color string
plate Plate
}

func Structs() {
fmt.Println("Creating car...")

car := Car{
name: "Honda Civic Type R",
color: "red",
plate: Plate{
letters: "ABC",
numbers: "1234",
},
}

fmt.Println(car)

car.color = "blue"

fmt.Println("The car has been painted to the color: [", car.color, "]")

//function
showPlate(car.plate)

//method
car.displayPlate()
}

//function
func showPlate(p Plate) {
fmt.Print("\n" + p.letters + "-" + p.numbers + "\n")
}

//method - struct Car
func (c Car) displayPlate() {
fmt.Print("\n" + c.plate.letters + "-" + c.plate.numbers + "\n")
}

0 comments on commit 8c10643

Please sign in to comment.