This is a creation in Article, where the information may have evolved or changed.
1. Preface
The Go language is a very powerful language to express. There is currently a restful webservice package implemented by Golang, go-restful is simple to use.
2.Demo
Example implementation of a query operation, a more detailed demo see here: Https://github.com/emicklei/go-restful/blob/master/examples/restful-user-resource.go
Package Mainimport ("Log" "net/http"//"StrConv" "github.com/emicklei/go-restful" "github.com/emicklei/go-restful/ Swagger ") type User struct {id,name string}type userresource struct {users map[string]user}func (U userresource) Register ( Container *restful. Container) {WS: = new (restful. WebService) ws. Path ("/users"). Doc ("Manage Users"). Consumes (restful. Mime_xml, restful. Mime_json). Produces (restful. Mime_json, restful. Mime_xml) ws. Route (ws. GET ("/{user-id}"). to (U.finduser).//Docsdoc ("Get a User"). Operation ("Finduser"). Param (ws. Pathparameter ("User-id", "identifier of the user"). DataType ("string")). Writes (user{}))//on the Responsecontainer. The ADD (WS)}func (U userresource) finduser (Request *restful. Request, Response *restful. Response) {id: = Request. Pathparameter ("User-id") usr: = U.users[id]if len (usr. Id) = = 0 {Response. AddHeader ("Content-type", "Text/plain") response. Writeerrorstring (http. Statusnotfound, "404:user could not be found.") Return}response. Writeentity (USR)}func main () {wscontainer: = restFul. Newcontainer () UserMap: = map[string]user{"1": user{"1", "Tom"}, "2": user{"2", "Jerry"},}u: = Userresource{usermap}u. Register (wscontainer) Config: = swagger. Config{webservices:wscontainer.registeredwebservices (),//control what Services is Visiblewebservicesurl: "http: localhost:8080 ", Apipath:"/apidocs.json ",//Optionally, specifiy where the UI is Locatedswaggerpath:"/apidoc S/", Swaggerfilepath:"/users/emicklei/xprojects/swagger-ui/dist "}swagger. Registerswaggerservice (config, wscontainer) log. Printf ("Start listening on localhost:8080") Server: = &http. SERVER{ADDR: "8080", Handler:wscontainer}log. Fatal (server. Listenandserve ())}3. Summary
Golang in the Go-restful library in the k8s as a apiserver rest framework to achieve the docking of routing and handler, the function is perfect.