douguanya4248 2016-03-02 03:44
浏览 1041
已采纳

在Golang中执行JSON Unmarshal时如何设置默认值到映射值?

I have a struct like this:

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)
    }
}

I can set Version = "1.0" as its default value, but how can I set default value to Width and Height?

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vivado如何支持多维打包数组模式
      • ¥15 请问第13题到底应该怎么做
      • ¥15 stable diffusion报错问题
      • ¥20 数据可视化综合运用 导入npz文件以及读取内容
      • ¥15 倒计时汇编语言RTC实时时钟
      • ¥15 CCS安装出现Failed to create the part's controls
      • ¥15 请问有digital Fortran编译器吗
      • ¥15 用cst怎么仿真同轴馈线
      • ¥15 如何在lammps中添加气液边界条件?
      • ¥20 具有每日记账功能的简易日历程序