2019-08-05 05:27
浏览 131


I have a Beam pipeline written in Go and I'd like to transform this input:

{"name": "Bob", "age": 32}

to a valid "object".

The go sdk has this function in the encoding package :

// UnmarshalJSON sets the state of this instance from the passed in JSON.
func (w *EncodedType) UnmarshalJSON(buf []byte) error {
    var s string
    if err := json.Unmarshal(buf, &s); err != nil {
        return err
    t, err := graphx.DecodeType(s)
    if err != nil {
        return err
    w.T = t
    return nil

But I don't understand how to use it as a step of my pipeline.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqie5529 2019-08-05 16:09

    You're sort of on the right track. Go doesn't have "objects" in the normal sense of the word as it pertains to programming. You would use a struct like this:

        type User struct {
            Name    string `json:"name"`
            Age     int    `json:"age"`

    You would use that to unmarshal into that structure like this:

        var myUser User
        err := json.Unmarshal([]byte(`{"name": "Bob", "age": 32}`), &myUser)
        if err != nil {
        fmt.Println(myUser.Name, myUser.Age)

    Here is a playground example:

    More information:

    解决 无用
    打赏 举报

相关推荐 更多相似问题