我有一个这样的结构:


package main


import (

    "encoding/json"

    "fmt"

)


type request struct {

    Version    string               `json:"version"`

    Operations map[string]operation `json:"operations"`

}

type operation struct {

    Type   string `json:"type"`

    Width  int    `json:"width"`

    Height int    `json:"height"`

}


func main() {

    jsonStr := "{\"version\": \"1.0\", \"operations\": {\"0\": {\"type\": \"type1\", \"width\": 100}, \"1\": {\"type\": \"type2\", \"height\": 200}}}"

    req := request{

         Version: "1.0",

    }

    err := json.Unmarshal([]byte(jsonStr), &req)

    if err != nil {

        fmt.Println(err.Error())

    } else {

        fmt.Println(req)

    }

}

我可以将 Version = "1.0" 设置为默认值,但如何将默认值设置为 Width 和 Height?