1 回答

TA貢獻1752條經驗 獲得超4個贊
使用原型圖:
message Test {
map<string, Service> services = 1;
}
message Service {
string command = 1;
string root = 2;
}
proto map 是在 Go中編譯的,因此在這種情況下,這是使用任意鍵對 JSON 建模的推薦方法。map[K]Vmap[string]*Service
這將給出以下輸出:
services:{key:"service1" value:{command:"command1" root:"/"}} services:{key:"service2" value:{command:"command2" root:"/"}}
示例程序:
package main
import (
"encoding/json"
"example.com/pb"
"fmt"
)
const file = `{
"services": {
"service1": {
"command": "command1",
"root": "/"
},
"service2": {
"command": "command2",
"root": "/"
}
}
}
`
func main() {
test := &pb.Test{}
err := json.Unmarshal([]byte(file), test)
if err != nil {
panic(err)
}
fmt.Println(test)
}
- 1 回答
- 0 關注
- 104 瀏覽
添加回答
舉報