Learn the basics of building web services and applications in Go.
- The standard library has much of what you need to build services and apps.
- The http package provides the building blocks. The most important types to learn right now are these
// A Request represents an HTTP request received by a server
// or to be sent by a client.
//
// Several fields are hidden from this example.
type Request struct {
Method string
URL *url.URL
Header Header
Body io.ReadCloser
}
// A ResponseWriter interface is used by an HTTP handler to
// construct an HTTP response.
type ResponseWriter interface {
Header() Header
Write([]byte) (int, error)
WriteHeader(int)
}
// A Handler responds to an HTTP request.
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
// ListenAndServe listens on the TCP network address addr
// and then calls Serve with handler to handle requests
// on incoming connections.
func ListenAndServe(addr string, handler Handler) errorhttps://golang.org/pkg/net/http/
https://golang.org/doc/articles/wiki/
Basic Web Handler
Routing Handlers
Using the Default Mux
Making Handlers out of Functions
Closures as HandlerFuncs
Servers are already Concurrent
The Response
Write a simple web service that has a set of different routes that return the string "Hello World" in multiple languages. Build the service using an Application context that will own the different handler methods. Then create your own mux, bind the routes and start the service. Validate your routes work in your browser.
All material is licensed under the Apache License Version 2.0, January 2004.