首页 > 代码库 > Go语言建立一个最简单的服务端点

Go语言建立一个最简单的服务端点

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)	}

  技术分享

Go语言建立一个最简单的服务端点