So my use case is this:
type Car struct {
Model string
Size int
}
func getSize(car Car) {
fmt.Println(car.Size)
}
func main() {
cars := make(map[string]*Car)
// fill cars with stuff
cars["Toyota"] = &Car{
Model: "Toyota",
Size: 2,
}
for _, car := range cars {
cars["Toyota"].Size = 4
}
for _, car := range cars {
//somehow get the value of car and insert into function
getSize(car)
}
}
but I'm not sure how to pass the value of a pointer from a map to a function since maps don't allow you to address the pointer.
Is there any way to better go about this?
You can dereference the cars map value during your call, which will invoke the function with the value pointed to by the car pointer:
getSize(*car)
If you don't you should receive a compilation error:
prog.go:31:16: cannot use car (type *Car) as type Car in argument to getSize
package main
import (
"fmt"
)
type Car struct {
Model string
Size int
}
func getSize(car Car) {
fmt.Println(car.Size)
}
func main() {
cars := make(map[string]*Car)
// fill cars with stuff
cars["Toyota"] = &Car{
Model: "Toyota",
Size: 2,
}
cars["Toyota"].Size = 4
for _, car := range cars {
//somehow get the value of car and insert into function
getSize(car)
}
}