亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

定義此嵌套映射時如何避免重復

定義此嵌套映射時如何避免重復

Go
汪汪一只貓 2022-09-26 15:42:43
在我的一個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
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

試試這個,如果那適合你更好


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

https://play.golang.org/p/6Kq5pvXYvNm


查看完整回答
反對 回復 2022-09-26
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

如何避免每次都定義?map[string]string


通過定義表示數據結構的類型。


type DeviceSchema struct {

    AdditionalProperties bool

    Properties Properties

    Type string

}


type Properties struct {

    Application string

    Hostname string

    IpAddress []map[string]string

    KernelVersion string

}


func main() {

    deviceSchemaData := DeviceSchema{

        AdditionalProperties: false,

        Properties: Properties{

            IpAddress: []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},

        },

        Type: "object",

    }

    fmt.Println(deviceSchemaData)

}

https://play.golang.org/p/iHfoft1zyAn


查看完整回答
反對 回復 2022-09-26
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

簡化定義的一種方法是:


func stringMap(s...string) map[string]string {

  ret:=map[string]string{}

  for i:=0; i<len(s);i+=2 {

     ret[s[i]]=s[i+1]

  }

  return ret

}


var deviceSchemaJson = map[string]interface{}{

    "additionalProperties": false,

    "properties": map[string]interface{}{

        "application": stringMap("type","string"),

        "hostname": stringMap("type","string"),

        "ipaddress": map[string]interface{}{

            "oneOf": []map[string]string{stringMap("format","ipv4"),stringMap("format","ipv6")},

            "type": "string",

        },

        "kernel_version": stringMap("type","string"),

    },

    "type": "object",

}


查看完整回答
反對 回復 2022-09-26
  • 3 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號