Go: invalid operation – type *map[key]value does not support indexing

You are trying to index on the pointer rather than the map itself. Kind of confusing because usually with pointers vs. values dereferencing is automatic for structs. If your struct is just a map, however, it’s only passed in by reference anyway so you don’t have to worry about creating methods that act on pointers to avoid copying the entire structure every time. The following code is equivalent to your first snippet but using a pointer type.

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

But to answer how to deal with it: if your struct is just of type map, I wouldn’t worry about writing your receiving functions to take pointers, and just receive the value since the value is only a reference anyways. Do like in your original snippet.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)