package main import ( "fmt" "github.com/stretchr/goweb/context" ) func main() { ctx := context.Background() ctx.Set("name", "John") name, ok := ctx.Get("name") if ok { fmt.Println(name) } }
package main import ( "net/http" "github.com/stretchr/goweb/context" ) func handler(w http.ResponseWriter, r *http.Request) { ctx := context.Get(r) name, ok := ctx.Get("name") if ok { fmt.Fprintf(w, "Hello, %s!", name) } else { fmt.Fprintf(w, "Hello!") } } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }In this example, we use the `context.Get()` function to retrieve the context that is associated with a given HTTP request (`r`), and then use `ctx.Get()` to retrieve a value ("name"). We then use `fmt.Fprintf()` to write a response to the client, including the retrieved name. This code can be run as a simple web application that listens on port 8080.