func helloHandler(c txweb.Context) error { name := c.Request().FormValue("name") return c.String(http.StatusOK, fmt.Sprintf("Hello, %s", name)) } func main() { router := txweb.NewRouter() router.GET("/hello", helloHandler) log.Fatal(txweb.ListenAndServe(":8080", router)) }
func personHandler(c txweb.Context) error { p := Person{Name: "Alice", Age: 25} return c.JSON(http.StatusOK, p) } type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { router := txweb.NewRouter() router.GET("/person", personHandler) log.Fatal(txweb.ListenAndServe(":8080", router)) }This example illustrates how to encode data in JSON format and send it back as the response to a GET request. Here, a simple Person struct is defined with Name and Age attributes. The handler encodes this data as JSON and sends it back to the client. In conclusion, the "github.com/garyburd/twister/web" package provides functions to handle HTTP request and response in Go, making it easier for developers to build web applications quickly and efficiently.