Dereferencing a map index in Golang

前端 未结 3 883
渐次进展
渐次进展 2020-12-03 11:27

I\'m learning Go currently and I made this simple and crude inventory program just to tinker with structs and methods to understand how they work. In the driver file I try t

相关标签:
3条回答
  • 2020-12-03 12:09

    As Volker said in his answer - you can't get address of an item in the map. What you should do - is to store pointers to items in your map, instead of storing item values:

    package main
    
    import "fmt"
    
    type item struct {
        itemName string
        amount   int
    }
    
    type Cashier struct {
        items map[int]*item
        cash  int
    }
    
    func (c *Cashier) Buy(itemNum int) {
        item, pass := c.items[itemNum]
    
        if pass {
            if item.amount == 1 {
                delete(c.items, itemNum)
            } else {
                item.amount--
            }
            c.cash++
        }
    }
    
    func (c *Cashier) AddItem(name string, amount int) {
        if c.items == nil {
            c.items = make(map[int]*item)
        }
        temp := &item{name, amount}
        index := len(c.items)
        c.items[index] = temp
    }
    
    func (c *Cashier) GetItems() map[int]*item {
        return c.items
    }
    
    func (i *item) GetName() string {
        return i.itemName
    }
    
    func (i *item) GetAmount() int {
        return i.amount
    }
    
    func main() {
        x := Cashier{}
        x.AddItem("item1", 13)
        f := x.GetItems()
        fmt.Println(f[0].GetAmount()) // 13
        x.Buy(0)
        f = x.GetItems()
        fmt.Println(f[0].GetAmount()) // 12
    }
    

    http://play.golang.org/p/HkIg668fjN

    0 讨论(0)
  • 2020-12-03 12:15

    While the other answers are useful, I think in this case it is best just to make non-mutating functions not take a pointer:

    func (i item) GetName() string{
        return i.itemName
    }
    
    func (i item) GetAmount() int{
        return i.amount
    }
    
    0 讨论(0)
  • 2020-12-03 12:19

    A map entry cannot be addressed (as its address might change during map growth/shrink), so you cannot call pointer receiver methods on them.

    Detail here: https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/4_pabWnsMp0

    0 讨论(0)
提交回复
热议问题