I just started learning golang. In a structure, I am using the below:
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)
}
}
In this code, I declared structures in main function both SellPram
and BuyPram
, but I think this is very redundant in the code. So is there a nice way to not declare both SellPram
and BuyPram
. I don't want to declare both of them, because at least one side will not use by the end of the function.
When explicitly converting a value from one struct type to another, as of Go 1.8 the tags are ignored. Thus two structs that differ only in their tags may be converted from one to the other:
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 }
This is how I do it. I have an Order
struct
. Buy
and Sell
struct
differences are an artifact of json
, which I hide from the rest of the program.
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)
}
Playground: https://play.golang.org/p/E2PL7wE3ls
Output:
{"pair":"twin","order_type":"sell","amount":"$1,000"}
{twin sell $1,000}
You may move common part to a separate structure and then embed it into the structures:
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")
}