Golang-通过接口分两步解组/重建对象

I'm working with json and golang. I've made a TCP server and, I need to Unmarshal the message to know which type of service is asked, before Unmarshal the contained data. It's a bit hard to explain so this is my code:

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)
}

I need to handle messages which comes up like it, but I don't know if it's possible? does it possible?

Thank for help !

There is json.RawMessage for this purpose.

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)
}

I might be wrong, but I don't think you can do it in one step.

First idea: Unmarshal in map[string]interface{}

Don't use type with unmarshal, instead use a map[string]interface{}, and then construct Selling/Buying from this map (or use directly the map)

type Container struct {
    Type string
    Object map[string]interface{}
}

Second idea: Two steps / clueless container

First: Unmarshall in a clueless Container that doesn't know the type

type CluelessContainer struct {
    Type string 
    Object interface{} `json:"-"` // or just remove this line ?
}

Then unmarshall in the type aware container. You could use a factory pattern to come with the right struct.