2016-04-23 36 views
1

我想知道是否有任何將地圖轉換爲GO中的結構。我遇到了稱爲地圖結構的包。然而,當我運行它,我的結構是沒有得到我的map.I值已下面貼轉換GO地圖到結構

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

我的代碼的相關位我有創建地圖[INT]字符串第一,換算成圖[字符串]字符串,然後將其轉換爲我的結構。請幫忙。我的結構返回0作爲鍵和空字符串作爲值。

輸出:

{ 
    "key": "", 
    "value": "" 
}{ 
    "key": "", 
    "value": "" 
+2

請運行'之前去fmt'您的代碼是什麼在這裏發佈,特別是在Go中,看到這樣的代碼格式是非常奇怪的 –

回答

0

MapStructure填充與字段相同的名稱的地圖的鍵的結構體。你的榜樣,因爲在地圖上都沒有x與按鍵名稱KeyValue產生零值({Key: 0, Value: ""}

將按以下你要不要?

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

那麼你建議我做什麼改變? 是的,這是正確的,我試圖保存所有值,以前插入和新的。 – RJP