Writing a basic HTTP server is easy using the
net/http
package.
packagemain
import("fmt""net/http")
A fundamental concept in
net/http
servers is
handlers
. A handler is an object implementing the
http.Handler
interface. A common way to write
a handler is by using the
http.HandlerFunc
adapter
on functions with the appropriate signature.
funchello(whttp.ResponseWriter,req*http.Request){
Functions serving as handlers take a
http.ResponseWriter
and a
http.Request
as
arguments. The response writer is used to fill in the
HTTP response. Here our simple response is just
“hello\n”.
We register our handlers on server routes using the
http.HandleFunc
convenience function. It sets up
the
default router
in the
net/http
package and
takes a function as an argument.