handlers/handlers.go
package handlers import ( "encoding/json" "net/http" ) func Routes() { http.HandleFunc("/sendjson", SendJSON) } func SendJSON(rw http.ResponseWriter, r *http.Request) { u := struct { Name string Email string }{ Name: "Bill", Email: "[email protected]", } rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(200) json.NewEncoder(rw).Encode(&u) }
main.go
package main import ( "log" "net/http" "handlers" ) func main() { handlers.Routes() log.Println("listener : Started : Listening on :4000") http.ListenAndServe(":4000", nil) }
时间: 2024-10-05 06:05:21