Go Web Development Net Http
Go Web Development Net Http
Introduction
Go's net/http package provides everything needed to build reliable and fast web servers with
minimal dependencies.
import (
"fmt"
"net/http"
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
Routing Basics
http.HandleFunc("/about", aboutHandler)
http.HandleFunc("/contact", contactHandler)
Serving Static Files
fs := http.FileServer(http.Dir("./static"))
return
name := r.FormValue("name")
log.Println(r.RequestURI)
next.ServeHTTP(w, r)
})
w.Header().Set("Content-Type", "application/json")
}
Structuring a Go Web Project
/project
/handlers
home.go
about.go
/static
/templates
main.go
Deployment Tips
- Use systemd, Docker, or nginx
Additional Resources
- https://pkg.go.dev/net/http
- https://gobyexample.com/
- https://gowebexamples.com