服务端
package main
import (
"easyGo/person"
"encoding/json"
"net/http"
)
func main() {
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
p := &person.Person{
Name: "jackie",
Age: 30,
T: person.T{
S: "hello world!",
},
}
resp := map[string]any{
"status": 0,
"message": "success",
"data": p,
}
jsonData, _ := json.Marshal(resp)
_, _ = w.Write(jsonData)
})
if err := http.ListenAndServe(":9090", nil); err != nil {
panic(err)
}
}
客户端
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func parse(buf []byte) {
var temp0 map[string]interface{}
err := json.Unmarshal(buf, &temp0)
if err != nil {
panic(err)
}
var temp1 = temp0["data"].(map[string]interface{})
var temp2 = temp1["t"].(map[string]interface{})
fmt.Println(temp1["name"], temp1["age"], temp2["s"])
}
func main() {
url := "http://127.0.0.1:9090/test"
resp, err := http.PostForm(url, nil)
if err != nil {
panic(err)
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
parse(buf)
}