【发布时间】: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)
}
}
在这段代码中,我在主函数中声明了SellPram 和BuyPram 的结构,但我认为这在代码中是非常多余的。
那么有没有一种不声明SellPram 和BuyPram 的好方法。
我不想同时声明它们,因为至少有一侧不会在函数结束时使用。
【问题讨论】:
-
为什么你用相同的字段创建了 2 个不同的结构?
-
最后一个json因子不一样,Sell最后一个是数量,而Buy是buy_amount。这需要将网站发布为尊重字段。
标签: go