Created
August 14, 2021 07:43
-
-
Save petrjahoda/e410747754309ee09983652f37c9cab3 to your computer and use it in GitHub Desktop.
3. Changing the name with a function, by using a pointer
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" | |
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