以下のようなコードを書いたときに
https://play.golang.org/p/JVvctX-ir-w
{}
を返ってくることを期待しているのに、 {"field":{}}
が返ってくる、ということにずっとハマっていました。
これは encoding/json
の omitempty
の振る舞いとして空のstructにomitemptyのtagを指定しても、空structとして扱ってしまって(値が入っているものとして扱ってしまって)key除外されないというものに起因していました。
The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string. json - The Go Programming Language
pointer型のstructにした場合には、key除外されます。
https://play.golang.org/p/YjD9D_Ot-Ot
そもそものomitemptyの挙動を理解してませんでした。。。。
omitempty
むずかしす。。。。