Golang: Having trouble with nested JSON Unmarshaler

后端 未结 2 638
误落风尘
误落风尘 2021-01-21 10:08

Given the following code:

package main

import (
    \"encoding/json\"
    \"log\"
)

type Somefin string

func (s *Somefin) UnmarshalJSON(b []byte) error {
             


        
2条回答
  •  太阳男子
    2021-01-21 11:09

    I figured this out.

    If you have the struct definition like this:

    type Wat struct {
        A, B string
        Somefin
    }
    

    Then the error I described in the OP happens. But if you do:

    type Wat struct {
        A, B string
        Somefin Somefin
    }
    

    Then it doesn't. Check out Chris's comment to this answer for a good explanation of why.

提交回复
热议问题