Go Web Examples

https://gowebexamples.com/arrow-up-right

Basic

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc(
        "/", 
        func(w http.ResponseWriter, r *http.Request) { // 1. A request handler
            fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
        }
    ) // 2. Registering a handler to the default HTTP Server

    http.ListenAndServe(":80", nil) // 3. start a HTTP server and listen for connections on port 80.
}

HTTP server

A basic HTTP server:

  1. process dynamic requests.

  2. serve static assets.

  3. listen on a specific port to be able to accept connections from the internet.

You can read GET parameters with r.URL.Query().Get("token") or POST form parameters with r.FormValue("email").

Routing

need gorilla/mux

Templates

automatic escaping = no need to worry about about XSS attacks.

{{.}}The dot is called the pipeline and the root element of the data.

Assets and Files

Last updated