How to do it...

In this recipe, we will define three routes, such as /, /login, and /logout along with their handlers. Perform the following steps:

  1. Create http-server-basic-routing.go and copy the following content:
package mainimport (  "fmt"  "log"  "net/http")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")func helloWorld(w http.ResponseWriter, r *http.Request) {  fmt.Fprintf(w, "Hello World!")}func login(w http.ResponseWriter, r *http.Request) {  fmt.Fprintf(w, "Login Page!")}func logout(w http.ResponseWriter, r *http.Request) {  fmt.Fprintf(w, "Logout Page!")}func main() {  http.HandleFunc("/", helloWorld)  http.HandleFunc("/login", login)  http.HandleFunc("/logout", logout)  err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil) if err ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.