Aller: opération non valide - type *carte[key]valeur ne prend pas en charge l'indexation

Je suis en train d'écrire une fonction qui modifie la carte originale qui est passé par pointeur, mais Aller ne le permet pas. Disons que j'ai une grosse carte et ne souhaitez pas copier d'avant en arrière.

Le code qui utilise le passage par valeur est de travailler et de faire ce dont j'ai besoin, mais implique le passage par valeur (aire de jeux):

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)
}

Mais si j'essaie de passer le paramètre comme pointeur comme ici (aire de jeux):

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
}

Je suis d'erreur de compilation:

prog.go:15: invalid operation: b[amount.Currency] (type *Balance does not support indexing)
prog.go:17: invalid operation: b[amount.Currency] (type *Balance does not support indexing)
prog.go:19: invalid operation: b[amount.Currency] (type *Balance does not support indexing)

Comment dois-je faire avec cela?

OriginalL'auteur Alexander Trakhimenok | 2016-04-06