Skip to content

Instantly share code, notes, and snippets.

@petrjahoda
Created August 14, 2021 07:43
Show Gist options
  • Save petrjahoda/e410747754309ee09983652f37c9cab3 to your computer and use it in GitHub Desktop.
Save petrjahoda/e410747754309ee09983652f37c9cab3 to your computer and use it in GitHub Desktop.
3. Changing the name with a function, by using a pointer
package main
import "fmt"
type Car struct {
name string
}
type Truck struct {
name string
}
func main() {
bmw := Car{name: "bmw is the best car"}
volvo := Truck{name: "volvo is the best truck"}
bmw.name = changeNameOfCar(bmw)
volvo.name = changeNameOfTruck(volvo)
changeNameOfCarUsingPointer(&bmw)
changeNameOfTruckUsingPointer(&volvo)
fmt.Println(bmw.name)
fmt.Println(volvo.name)
}
func changeNameOfCar(c Car) string {
return c.name + "\n\t... changed with simple function"
}
func changeNameOfTruck(t Truck) string {
return t.name + "\n\t... changed with simple function"
}
func changeNameOfCarUsingPointer(c *Car) {
c.name = c.name + "\n\t... changed using pointer"
}
func changeNameOfTruckUsingPointer(t *Truck) {
t.name = t.name + "\n\t... changed using pointer"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment