A channel can be used to interchange data with the pre-compiled runtime and therefore objects impletementing interfaces must be wrapped if necessary, using genInterfaceWrapper. A similar treatment could be applied when sending interpreted functions over a channel, to be provided in a new PR. Fixes #1010.
23 lines
369 B
Go
23 lines
369 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type MyJsonMarshaler struct{ n int }
|
|
|
|
func (m MyJsonMarshaler) MarshalJSON() ([]byte, error) {
|
|
return []byte(fmt.Sprintf(`{"num": %d}`, m.n)), nil
|
|
}
|
|
|
|
func main() {
|
|
ch := make(chan json.Marshaler, 1)
|
|
ch <- MyJsonMarshaler{2}
|
|
m, err := json.Marshal(<-ch)
|
|
fmt.Println(string(m), err)
|
|
}
|
|
|
|
// Output:
|
|
// {"num":2} <nil>
|