mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
51 lines
892 B
Go
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] = ¤t
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|