码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-15 22:18:42      阅读:1214      评论:0      收藏:0      [点我收藏+]

标签:val   post   AC   ati   ret   indexing   index   struct   float   

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

  

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

标签:val   post   AC   ati   ret   indexing   index   struct   float   

原文地址:https://www.cnblogs.com/allenhaozi/p/8576228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!