N
N
Nube2018-03-04 14:24:54
go
Nube, 2018-03-04 14:24:54

Analog json.RawMessage for structures?

It is necessary for me that in structure I could put any other structure. Something like

type Packet struct {
  Type  string
  Body  json.RawMessage
}

but for an ordinary structure like
type Page struct {
  Title	string
  Body   *struct{}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2018-03-04
@Nube

There is an empty interface specifically for this.

type Page struct {
  Title string
  Body  interface{}
}

In a field of this type, you can put a value of any type in general. If you need to get it from there, you can cast it to the original type via type assertion
. If you just then convert the Page structure to json, then you don’t have to bring anything, json.Marshal itself can do it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question