dictionary - 定义此嵌套映射时如何避免重复
问题描述
在我的一个 json 模式中,我有一张这样的地图
var deviceSchemaJson = map[string]interface{}{
"additionalProperties": false,
"properties": map[string]interface{}{
"application": map[string]string{
"type": "string",
},
"hostname": map[string]string{
"type": "string",
},
"ipaddress": map[string]interface{}{
"oneOf": []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
"type": "string",
},
"kernel_version": map[string]string{
"type": "string",
},
},
"type": "object",
}
我怎样才能避免map[string]string
每次都定义?
解决方案
如果这更适合你,试试这个
package main
import (
"fmt"
)
func main() {
fmt.Printf("%#v\n",deviceSchemaJson)
}
var deviceSchemaJson = value{
"additionalProperties": false,
"properties": value{
"application": value{
"type": "string",
},
"hostname": value{
"type": "string",
},
"ipaddress": value{
"oneOf": []valuestring{{"format": "ipv4"}, {"format": "ipv6"}},
"type": "string",
},
"kernel_version": valuestring{
"type": "string",
},
},
"type": "object",
}
type value map[string]interface{}
type valuestring map[string]string
推荐阅读
- go - 无法使频道地图正常工作
- ios - 通过 Swift 中的 segue 将点击的单元格数据传递到另一个视图
- haskell - 请求/响应 Cassandra
- phpstorm - 如何在 Windows 的命令行中打开 PhpStorm 中的项目
- c# - Unity3d - 调用公共静态变量性能
- webrtc - 为什么我的 peerconnection 不生成候选冰?
- c# - 实体框架模拟需要全局上下文
- ethereum - 如何在实体映射中使用字符串数组?
- css - 在 react-slick 中,className 与 webpack css-loader 转换的 CSS 文件中的类名不匹配
- symfony - 如何在 symfony 中通过电子邮件发送控制台错误