में एक मैप इंडेक्स को डिफ्रेंस करना मैं वर्तमान में जा रहा हूं सीख रहा हूं और मैंने यह सरल और कच्चे माल कार्यक्रम को केवल structs और विधियों के साथ टिंकर करने के लिए बनाया है ताकि यह समझ सके कि वे कैसे काम करते हैं। ड्राइवर फ़ाइल में मैं कैशियर प्रकार के आइटम मैप से विधि और आइटम प्रकार से एक विधि कॉल करने का प्रयास करता हूं। मेरी विधि में कॉपी बनाने के बजाय सीधे structs का उपयोग करने के लिए पॉइंटर रिसीवर है।गोलांग
package inventory
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.items[itemNum] = item
}
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
}
Driver.go:: जब मैं कार्यक्रम चलाने मैं यह त्रुटि .\driver.go:11: cannot call pointer method on f[0] .\driver.go:11: cannot take the address of f[0]
Inventory.go मिल
package main
import "fmt"
import "inventory"
func main() {
x := inventory.Cashier{}
x.AddItem("item1", 13)
f := x.GetItems()
fmt.Println(f[0].GetAmount())
}
कोड है कि वास्तव में मेरी समस्या से संबंधित है के भाग के GetAmount
है inventory.go
में फ़ंक्शन और driver.go