zoukankan      html  css  js  c++  java
  • Go: invalid operation

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

      

  • 相关阅读:
    第8周课下作业1(补)
    第八章课下测试
    POJ
    POJ
    HDU
    UVa
    UVa
    CodeForces
    ZOJ
    LightOJ
  • 原文地址:https://www.cnblogs.com/allenhaozi/p/8576228.html
Copyright © 2011-2022 走看看