Go 关于高尔夫,有没有一个好的方法来避免代码中的冗余?
我刚开始学咕噜。在结构中,我使用以下内容:Go 关于高尔夫,有没有一个好的方法来避免代码中的冗余?,go,Go,我刚开始学咕噜。在结构中,我使用以下内容: 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"`
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)
}
}
在这段代码中,我在main函数SellPram
和BuyPram
中声明了结构,但我认为这在代码中是非常冗余的。
那么,有没有一种好方法不同时声明SellPram
和BuyPram
。
我不想同时声明它们,因为至少有一方在函数结束时不会使用
将值从一种结构类型显式转换为另一种结构类型时,如
在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
}
我就是这样做的。我有一个
订单
结构
Buy
和Sell
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)
}
游乐场:
输出:
{"pair":"twin","order_type":"sell","amount":"$1,000"}
{twin sell $1,000}
将值从一种结构类型显式转换为另一种结构类型时,如
在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
}
我就是这样做的。我有一个
订单
结构
Buy
和Sell
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)
}
游乐场:
输出:
{"pair":"twin","order_type":"sell","amount":"$1,000"}
{twin sell $1,000}
您可以将公共零件移动到单独的结构中,然后将其嵌入到结构中:
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")
}
您可以将普通零件移动到单独的结构中,然后将其嵌入结构中:
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")
}
为什么创建了两个具有相同字段的不同结构?最后一个json因子不相同,最后一个是销售金额,但购买是购买金额。这需要将网站发布为差异字段。为什么您创建了两个具有相同字段的不同结构?最后一个json因子不相同,最后一个是销售,但购买是购买金额。这需要将网站发布为“尊重”字段。