omitempty

在这里插入图片描述
id和order输出正常
在这里插入图片描述

id为0 order为""没有json字符串中没有输出

package main

import (
	"encoding/json"
	"fmt"
)

var jsonBlob = []byte(`[
    {"Name": "c++", "Order": "1","id":0},
    {"Name": "lua", "Order": ""},
    {"Name": "golang"}
]`)

type Book struct {
	Name  string `json:"name"`
	Order string `json:"order,omitempty"`
	ID    int    `json:"id,omitempty"`
}

func main() {

	var books []Book
	err := json.Unmarshal(jsonBlob, &books)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v \n", books)

	data, err := json.Marshal(books)
	if err != nil {
		fmt.Printf(err.Error())
		return
	}

	fmt.Printf("data:%v \n", string(data))

}