Ctx
Overview
Ctx wraps a single HTTP request and response.
Part of the http package — Node.js analog: express, cors, helmet, morgan.
Ctx is a type exported by gox. Methods on this type are documented separately.
Signature
go
type Ctx struct {
ResponseWriter http.ResponseWriter
Request *http.Request
// contains filtered or unexported fields
}Compare: Node.js · Standard Go · gox
js
// Typical express, cors, helmet, morgan pattern in Node.jsgo
func handler(w http.ResponseWriter, r *http.Request) {
// chi or net/http
}go
import "github.com/sahilkhaire/gox/http"
func handler(c *http.Ctx) error {
return c.JSON(200, map[string]string{"hello": "world"})
}Example
go
import "github.com/sahilkhaire/gox/http"
func handler(c *http.Ctx) error {
return c.JSON(200, map[string]string{"hello": "world"})
}Tips
Stack Logger, Recover, and Security middleware the way you would morgan + helmet in Express.
Standard library alternative
Use the standard library directly:
go
func handler(w http.ResponseWriter, r *http.Request) {
// chi or net/http
}