The Go language implements simple Web server methods

  • 2020-05-26 09:17:34
  • OfStack

This article illustrates how the Go language implements a simple Web server. Share with you for your reference. The specific analysis is as follows:

The package http responds to HTTP requests with any value that implements http.Handler:
package http
type Handler interface {
ServeHTTP(w ResponseWriter,
r *Request)
}

In this example, the type Hello implements http.Handler.
Note: this example does not work on the web-based tutorial user interface. To try to write an web server, you might need to install Go.

package main
import (
    "fmt"
    "net/http"
)
type Hello struct{}
func (h Hello) ServeHTTP(
        w http.ResponseWriter,
        r *http.Request) {
    fmt.Fprint(w, "Hello!")
}
func main() {
    var h Hello
    http.ListenAndServe("localhost:4000",h)
}

I hope this article has helped you with the Go programming language.


Related articles: