gpt4 book ai didi

object - Golang - 通过接口(interface)通过 2 步解码/重建对象

转载 作者:IT王子 更新时间:2023-10-29 01:49:39 24 4
gpt4 key购买 nike

我正在使用 json 和 golang。我已经创建了一个 TCP 服务器,在解码包含的数据之前,我需要解码消息以了解请求的服务类型。这有点难以解释,所以这是我的代码:

package main

import (
"fmt"
"encoding/json"
)

type Container struct {
Type string
Object interface{}
}

type Selling struct {
Surname string
Firstname string
//......
Price int
}

type Buying struct {
ID int
Surname string
Firstname string
//..........
}

/*
type Editing struct {
ID int
...............
}
Informations, etc etc
*/

func main() {

tmp_message_json1 := Selling{Surname: "X", Firstname: "Mister", Price: 10}
//tmp_message_json1 := Buying{ID: 1, Surname: "X", Firstname: "Mister"}
tmp_container_json1 := Container{Type: "Selling", Object: tmp_message_json1}
json_tmp, _ := json.Marshal(tmp_container_json1)

/*...........
We init tcp etc etc and then a message comes up !
...........*/

c := Container{}
json.Unmarshal(json_tmp, &c)
//I unmarshal a first time to know the type of service asked

// first question: Does Unmarshal need to be used only one time?
// does I need to pass c.Object as a string to unmarshal it in the called functions?
if c.Type == "Buying" {
takeInterfaceBuying(c.Object)
} else if c.Type == "client" {
takeInterfaceSelling(c.Object)
} else {
fmt.Println("bad entry")
}
}

func takeInterfaceBuying(Object interface{}) {

bu := Object

fmt.Println(bu.Firstname, bu.Surname, "wants to buy the following product:", bu.ID)
}

func takeInterfaceSelling(Object interface{}) {

se := Object

fmt.Println(se.Firstname, se.Surname, "wants to sell something for:", se.Price)
}

我需要处理这样出现的消息,但我不知道这是否可能?可能吗?

感谢帮助!

最佳答案

有 json.RawMessage 用于此目的。

package main

import (
"encoding/json"
"fmt"
)

type Container struct {
Type string
Object json.RawMessage
}

type Selling struct {
Surname string
Firstname string
Price int
}

type Buying struct {
ID int
Surname string
Firstname string
}

func main() {
rawJson1 := []byte(`{"Type":"Selling","Object":{"Surname":"X","Firstname":"Mister","Price":10}}`)
rawJson2 := []byte(`{"Type":"Buying","Object":{"ID":1,"Surname":"X","Firstname":"Mister"}}`)

processMessage(rawJson1)
processMessage(rawJson2)
}

func processMessage(data []byte) {
var c Container
json.Unmarshal(data, &c)

switch {
case c.Type == "Buying":
processBuying(c)
case c.Type == "Selling":
processSelling(c)
default:
fmt.Println("bad entry")
}
}

func processBuying(c Container) {
var bu Buying
json.Unmarshal(c.Object, &bu)
fmt.Println(bu.Firstname, bu.Surname, "wants to buy the following product:", bu.ID)
}

func processSelling(c Container) {
var se Selling
json.Unmarshal(c.Object, &se)
fmt.Println(se.Firstname, se.Surname, "wants to sell something for:", se.Price)
}

关于object - Golang - 通过接口(interface)通过 2 步解码/重建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37458556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com