【问题标题】:About golfing, Is there a nice way to not have redundancy in this code?关于打高尔夫球,有没有一种很好的方法可以在这段代码中没有冗余?
【发布时间】:2017-11-27 14:12:43
【问题描述】:

我刚开始学习 golang。在一个结构中,我使用以下内容:

type Sell struct {
    Pair string      `json:"pair"`
    OrderType string `json:"order_type"`
    Amount string    `json:"amount"`
}

type Buy struct {
    Pair string      `json:"pair"`
    OrderType string `json:"order_type"`
    Amount string    `json:"buy_amount"`
}

func CreateSomething(a, b, c, OrderType string) {
    SellPram := Sell{}
    BuyPram  := Buy{}
    if OrderType == "sell" {
        SellPram = Sell{a,b,c}
        json.Marshal(SellPram)
    } else if OrderType == "buy" {
        BuyPram = Buy{a,b,c}
        json.Marshal(BuyPram)
    }
}

在这段代码中,我在主函数中声明了SellPramBuyPram 的结构,但我认为这在代码中是非常多余的。 那么有没有一种不声明SellPramBuyPram 的好方法。 我不想同时声明它们,因为至少有一侧不会在函数结束时使用。

【问题讨论】:

  • 为什么你用相同的字段创建了 2 个不同的结构?
  • 最后一个json因子不一样,Sell最后一个是数量,而Buy是buy_amount。这需要将网站发布为尊重字段。

标签: go


【解决方案1】:

Go 1.8 Release Notes

Changes to the language

当显式地将值从一种结构类型转换为另一种时,如 Go 1.8 的标签被忽略。因此,两个仅在以下方面不同的结构 他们的标签可能会从一个转换为另一个:

func example() {
  type T1 struct {
      X int `json:"foo"`
  }
  type T2 struct {
      X int `json:"bar"`
  }
  var v1 T1
  var v2 T2
  v1 = T1(v2) // now legal
}

这就是我的做法。我有一个Order structBuySell struct 的差异是 json 的产物,我对程序的其余部分隐藏了它。

package main

import (
    "encoding/json"
    "fmt"
)

type Order struct {
    Pair      string `json:"pair"`
    OrderType string `json:"order_type"`
    Amount    string `json:"amount"`
}

func CreateSomething(pair, amount, orderType string) Order {
    type Sell struct {
        Pair      string `json:"pair"`
        OrderType string `json:"order_type"`
        Amount    string `json:"amount"`
    }
    type Buy struct {
        Pair      string `json:"pair"`
        OrderType string `json:"order_type"`
        Amount    string `json:"buy_amount"`
    }

    var order Order
    var buf []byte
    switch orderType {
    case "sell":
        sell := Sell{pair, orderType, amount}
        buf, _ = json.Marshal(sell)
        order = Order(sell)
    case "buy":
        buy := Buy{pair, orderType, amount}
        buf, _ = json.Marshal(buy)
        order = Order(buy)
    }
    fmt.Println(string(buf))
    return order
}

func main() {
    order := CreateSomething("twin", "$1,000", "sell")
    fmt.Println(order)
}

游乐场:https://play.golang.org/p/E2PL7wE3ls

输出:

{"pair":"twin","order_type":"sell","amount":"$1,000"}
{twin sell $1,000}

【讨论】:

    【解决方案2】:

    您可以将公共部分移动到单独的结构中,然后将其嵌入到结构中:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Order struct {
        Pair      string `json:"pair"`
        OrderType string `json:"order_type"`
    }
    
    type Sell struct {
        Order
        Amount string `json:"amount"`
    }
    
    type Buy struct {
        Order
        Amount string `json:"buy_amount"`
    }
    
    func CreateSomething(a, b, c, OrderType string) {
        SellPram := Sell{}
        BuyPram := Buy{}
        if OrderType == "sell" {
            SellPram = Sell{Order{Pair: a, OrderType: b}, c}
            s, err := json.Marshal(SellPram)
            fmt.Println(s, err)
        } else if OrderType == "buy" {
            BuyPram = Buy{Order{a, b}, c}
            s, err := json.Marshal(BuyPram)
            fmt.Println(string(s), err)
        }
    }
    
    func main() {
        fmt.Println("Hello, playground")
        CreateSomething("a", "b", "c", "buy")
    }
    

    https://play.golang.org/p/aTjSRkMVqr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多