使用Go搭建服务比较方便,引入net/http即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package main
import ( "fmt" "net/http" "encoding/json" )
type Data struct{ Name string `json:"name"` Age int `json:"age"` Sex bool `json:"sex"` }
func testApi(w http.ResponseWriter, r *Request){ data := Data{ Name:"sonderss", Age:24, Sex:true, } r.ParseForm()
jsonStu, _ := json.Marshal(data)
w.Write(jsonStu) }
func main(){ http.HandleFunc("/",testApi) http.ListenAndServe(":8005", nil) }
|