2013-11-26 25 views
8

में एक मैप इंडेक्स को डिफ्रेंस करना मैं वर्तमान में जा रहा हूं सीख रहा हूं और मैंने यह सरल और कच्चे माल कार्यक्रम को केवल 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

उत्तर

19

में प्रिंट स्टेटमेंट को मानचित्र प्रविष्टि को संबोधित नहीं किया जा सकता है (जैसा कि उसका पता माइग्रेट किया गया है मानचित्र वृद्धि/संकीर्णता के दौरान एचटी परिवर्तन), ताकि आप उन पर पॉइंटर रिसीवर विधियों को कॉल न कर सकें।

यहाँ विवरण: https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/4_pabWnsMp0

12

के रूप में वोल्कर अपने जवाब में कहा - आप नक्शे में किसी आइटम का पता नहीं मिल सकता है। क्या करना चाहिए - के बजाय आइटम मूल्यों को संग्रहीत करने का, अपने नक्शे में आइटमों की ओर इशारा स्टोर करने के लिए है:

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

जबकि अन्य उत्तर उपयोगी होते हैं, मैं इस मामले में लगता है कि यह सिर्फ करने के लिए सबसे अच्छा है

func (i item) GetName() string{ 
    return i.itemName 
} 

func (i item) GetAmount() int{ 
    return i.amount 
} 
: गैर परिवर्तनशील कार्यों नहीं एक सूचक ले कर