handlers/handlers.go
package handlersimport ( "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: "bill@email.com", } rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(200) json.NewEncoder(rw).Encode(&u)}
main.go
package mainimport ( "log" "net/http" "handlers")func main() { handlers.Routes() log.Println("listener : Started : Listening on :4000") http.ListenAndServe(":4000", nil) }