2 回答

TA貢獻1864條經驗 獲得超6個贊
您正在嘗試索引指針而不是地圖本身。有點令人困惑,因為通常指針與值解引用對于結構來說是自動的。但是,如果您的結構只是一個映射,那么它無論如何只能通過引用傳入,因此您不必擔心創建對指針起作用的方法以避免每次都復制整個結構。以下代碼等效于您的第一個代碼段,但使用的是指針類型。
package main
import "fmt"
type Currency string
type Amount struct {
Currency Currency
Value float32
}
type Balance map[Currency]float32
func (b *Balance) Add(amount Amount) *Balance {
current, ok := (*b)[amount.Currency]
if ok {
(*b)[amount.Currency] = current + amount.Value
} else {
(*b)[amount.Currency] = amount.Value
}
return b
}
func main() {
b := &Balance{Currency("USD"): 100.0}
b = b.Add(Amount{Currency: Currency("USD"), Value: 5.0})
fmt.Println("Balance: ", (*b))
}
但是要回答如何處理它:如果您的結構只是映射類型,我不會擔心編寫您的接收函數來獲取指針,并且只接收值,因為無論如何該值只是一個引用。在你的原始片段中做喜歡。

TA貢獻1765條經驗 獲得超5個贊
您可以簡單地取消引用b:(*b)
https://play.golang.org/p/Xq6qFy4_PC
func (b *Balance) Add(amount Amount) *Balance {
current, ok := (*b)[amount.Currency]
if ok {
(*b)[amount.Currency] = current + amount.Value
} else {
(*b)[amount.Currency] = amount.Value
}
return b
}
更新
@Serdmanczyk 提出了一個很好的觀點......您可以安全地按值傳遞地圖,底層地圖將被更新,而不是地圖的副本。也就是說; 在映射的情況下按值傳遞意味著傳遞映射的地址,而不是映射的內容。
見https://play.golang.org/p/i7Yz4zMq4v
type foo map[string]string
func main() {
a := foo{}
a["hello"] = "world"
fmt.Printf("%#v\n", a)
mod(a)
fmt.Printf("%#v\n", a)
}
func mod(f foo) {
f["hello"] = "cruel world"
}
哪些輸出:
main.foo{"hello":"world"}
main.foo{"hello":"cruel world"}
- 2 回答
- 0 關注
- 233 瀏覽
添加回答
舉報