这个问题在这里已有答案:
我是一个将应用程序转换为Go的新用户。我有类似以下的工作:
type Network struct {
Ssid string
Security string
Bitrate string
}
func Scan(w http.ResponseWriter, r *http.Request) {
output := runcmd(scripts+"scan.sh", true)
bytes := []byte(output)
var networks []Network
json.Unmarshal(bytes, &networks)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(networks)
}
问题是旧版本没有在返回的json变量上使用大写字母。
我希望前端看到ssid
而不是Ssid
。如果我将结构中的属性设置为小写,则代码不再起作用,因为它们成为未导出的变量。
这个工具非常方便学习:
https://mholt.github.io/json-to-go/
给它你希望它推荐golang的JSON示例。
如JSON
{
"ssid": "some very long ssid",
"security": "big secret",
"bitrate": 1024
}
golang会建议:
type AutoGenerated struct {
Ssid string `json:"ssid"`
Security string `json:"security"`
Bitrate int `json:"bitrate"`
}
现在你可以将AutogGenerated, Ssid, Security, Bitrate
改为你想要的任何东西。