gpt4 book ai didi

rest - 将 GO map 转换为结构

转载 作者:IT王子 更新时间:2023-10-29 01:47:56 25 4
gpt4 key购买 nike

我想知道在 GO 中是否有将映射转换为结构的方法。我遇到了名为 map structure 的包。但是,当我运行它时,我的结构没有获取我的 map 的值。我已经在下面发布了我的代码的相关部分

package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"

"github.com/drone/routes"
"github.com/mitchellh/mapstructure"
)

var Infos = []Info{}

type Info struct {
Keyy int `json:"key"`
Valuee string `json:"value"`
}

var m map[int]string
var outgoingJSON string
var x map[string]string

func main() {
m = make(map[int]string)
x = make(map[string]string)
mux := routes.New()
mux.Put("/:key1/:value1", PutData)

mux.Get("/profile", GetProfile)
http.Handle("/", mux)
http.ListenAndServe(":3000", nil)
}
func PutData(w http.ResponseWriter, r *http.Request) {

key_data := r.URL.Query().Get(":key1")
value_data := r.URL.Query().Get(":value1")
key2, err := strconv.Atoi(key_data)
if err != nil {
panic(err)
} else {
m[key2] = value_data
}

for key2, value_data := range m {
log.Println(key2, value_data)
log.Println(m)
x[strconv.FormatInt(int64(key2), 10)] = value_data
log.Println(x)
var result Info
err1 := mapstructure.Decode(x, &result)
if err1 != nil {
panic(err1)
}
Infos = append(Infos, result)
//log.Println(key2,value_data)
// outgoingJSON, err := json.MarshalIndent(x,""," ")
// if err != nil {
// panic(err)
// break
// }

}
w.WriteHeader(204)

//log.Println(key_data)
}
func GetProfile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

for _, p := range Infos {

outgoingJSON, err := json.MarshalIndent(p, "", " ")
if err != nil {
panic(err)
break
}
w.WriteHeader(200)
fmt.Fprint(w, string(outgoingJSON))
}
}

我首先创建了一个 map[int]string,将其转换为 map[string]string,然后将其转换为我的结构。请帮忙。我的结构返回 0 作为键和空字符串作为值。

输出:

{
"key": "",
"value": ""
}{
"key": "",
"value": ""

最佳答案

MapStructure 使用与 map 键同名的字段填充结构。您的示例生成零值( {Key: 0, Value: ""} 因为 map x 中没有名称为 Key

下面会做你想做的事吗?

package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/drone/routes"
)

// Infos is a slice of Info
var Infos = []Info{}

// Info is a Key, Value struct
type Info struct {
Key int `json:"key"`
Value string `json:"value"`
}

func main() {
mux := routes.New()
mux.Put("/:key1/:value1", PutData)

mux.Get("/profile", GetProfile)
http.Handle("/", mux)
http.ListenAndServe(":3000", nil)
}

// PutData puts the new key,values in to Infos
func PutData(w http.ResponseWriter, r *http.Request) {

keyData := r.URL.Query().Get(":key1")
valueData := r.URL.Query().Get(":value1")
key2, err := strconv.Atoi(keyData)
if err != nil {
panic(err)
} else {
Infos = append(Infos, Info{
Key: key2,
Value: valueData,
})
}

w.WriteHeader(204)

}

// GetProfile displays the Infos
func GetProfile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

for _, p := range Infos {

outgoingJSON, err := json.MarshalIndent(p, "", " ")
if err != nil {
panic(err)
break
}
w.WriteHeader(200)
fmt.Fprint(w, string(outgoingJSON))
}
}

关于rest - 将 GO map 转换为结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com