mirror of
https://github.com/gytisrepecka/go-learning
synced 2024-12-03 16:33:35 +02:00
Added basic-struct.go with example of using struct in struct, also calling method.
This commit is contained in:
parent
14079d51e7
commit
bb840a479c
1 changed files with 78 additions and 0 deletions
78
basic-struct.go
Normal file
78
basic-struct.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2018 Gytis Repečka. All rights reserved.
|
||||
// Use of this source code is governed by a GNU GPL
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Define named structures
|
||||
|
||||
type CarMake struct {
|
||||
makeTitle string
|
||||
countryOfOrigin string
|
||||
}
|
||||
|
||||
type Car struct {
|
||||
make CarMake
|
||||
model string
|
||||
year int
|
||||
started bool
|
||||
}
|
||||
|
||||
// Functions (methods)
|
||||
|
||||
// Receiving a pointer to Car so can modify it
|
||||
func (c *Car) Start() {
|
||||
c.started = true
|
||||
fmt.Printf("Car %s %s says vrooom!\n", c.make.makeTitle, c.model)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var carMakes []CarMake
|
||||
newCarMake := CarMake{makeTitle: "Toyota", countryOfOrigin: "Japan"}
|
||||
carMakes = append(carMakes, newCarMake)
|
||||
newCarMake = CarMake{makeTitle: "Renault", countryOfOrigin: "France"}
|
||||
carMakes = append(carMakes, newCarMake)
|
||||
newCarMake = CarMake{"Peugeot", "France"}
|
||||
carMakes = append(carMakes, newCarMake)
|
||||
|
||||
var cars []Car
|
||||
newCar := Car{carMakes[1], "Clio", 1994, false}
|
||||
cars = append(cars, newCar)
|
||||
newCar = Car{carMakes[2], "406", 1997, false}
|
||||
cars = append(cars, newCar)
|
||||
newCar = Car{carMakes[0], "Yaris", 2000, false}
|
||||
cars = append(cars, newCar)
|
||||
|
||||
fmt.Println("--------------------------")
|
||||
fmt.Println("Make | Model | Country")
|
||||
fmt.Println("--------------------------")
|
||||
|
||||
for i:=0; i<len(cars); i++ {
|
||||
fmt.Printf("%s | %s | %s\n", cars[i].make.makeTitle, cars[i].model, cars[i].make.countryOfOrigin)
|
||||
}
|
||||
fmt.Println("--------------------------")
|
||||
|
||||
// Let's execute struct function (method)
|
||||
cars[2].Start()
|
||||
cars[0].Start()
|
||||
fmt.Println("--------------------------")
|
||||
|
||||
fmt.Println("Started cars:")
|
||||
startedCars := 0
|
||||
for i:=0; i<len(cars); i++ {
|
||||
if cars[i].started == true {
|
||||
fmt.Printf("%s %s\n", cars[i].make.makeTitle, cars[i].model)
|
||||
startedCars++
|
||||
}
|
||||
}
|
||||
if startedCars == 0 {
|
||||
fmt.Println("No cars are started.")
|
||||
} else {
|
||||
fmt.Printf("Total: %d\n", startedCars)
|
||||
}
|
||||
fmt.Println("--------------------------")
|
||||
}
|
Loading…
Reference in a new issue