100-go-mistakes/4-control-structures/32-range-loop-pointers/customer-store/main.go
2021-12-27 15:56:17 +01:00

51 lines
892 B
Go

package main
import "fmt"
type Customer struct {
ID string
Balance float64
}
type Store struct {
m map[string]*Customer
}
func main() {
s := Store{
m: make(map[string]*Customer),
}
s.storeCustomers([]Customer{
{ID: "1", Balance: 10},
{ID: "2", Balance: -10},
{ID: "3", Balance: 0},
})
print(s.m)
}
func (s *Store) storeCustomers(customers []Customer) {
for _, customer := range customers {
fmt.Printf("%p\n", &customer)
s.m[customer.ID] = &customer
}
}
func (s *Store) storeCustomers2(customers []Customer) {
for _, customer := range customers {
current := customer
s.m[current.ID] = &current
}
}
func (s *Store) storeCustomers3(customers []Customer) {
for i := range customers {
customer := &customers[i]
s.m[customer.ID] = customer
}
}
func print(m map[string]*Customer) {
for k, v := range m {
fmt.Printf("key=%s, value=%#v\n", k, v)
}
}