I get string through rabbitmq message system. Before sending,
I use json.Marshal , convert the result to string and send via RabbitMQ.
The structures that I convert and submit can be: (the names and sizes of the structures are changed, but it does not matter)
type Somthing1 struct{ Thing string `json:"thing"` OtherThing int64 `json:"other_thing"` }
or
type Somthing2 struct{ Croc int `json:"croc"` Odile bool `json:"odile"` }
The message goes fine as a string and prints on the other side (on some server)
Everything is still working. Now I am trying to convert them to my structures and assert types.
first try:
func typeAssert(msg string) { var input interface{} json.Unmarshal([]byte(msg), &input) switch input.(type){ case Somthing1: job := Somthing1{} job = input.(Somthing1) queueResults(job) case Somthing2: stats := Somthing2{} stats = input.(Somthing2) queueStatsRes(stats) default: }
This does not work. When I type input after canceling the cancellation, I get map[string]interface{} (?!?)
and even stranger, the card key is the string I received, and the card value is empty.
I made several other attempts:
func typeAssert(msg string) { var input interface{} json.Unmarshal([]byte(msg), &input) switch v := input.(type){ case Somthing1: v = input.(Somthing1) queueResults(v) case Somthing2: v = input.(Somthing2) queueStatsRes(v) default: }
and also tried to write a switch, as explained in this answer: Golang: cannot enter a key on a value without an interface
switch v := interface{}(input).(type)
still without success ...
Any ideas?