import ( "github.com/emicklei/go-restful" "net/http" ) func HelloWorld(req *restful.Request, res *restful.Response) { res.Write([]byte("Hello, world!")) } func main() { ws := new(restful.WebService) ws.Route(ws.GET("/hello").To(HelloWorld)) restful.Add(ws) http.ListenAndServe(":8080", nil) }
import ( "github.com/emicklei/go-restful" "net/http" ) func Greet(req *restful.Request, res *restful.Response) { name := req.PathParameter("name") greeting := "Hello, " + name + "!" res.AddHeader("X-Greeting", greeting) res.Write([]byte(greeting)) } func main() { ws := new(restful.WebService) ws.Route(ws.GET("/greet/{name}").To(Greet)) restful.Add(ws) http.ListenAndServe(":8080", nil) }In this example, we define a GET endpoint `/greet/{name}` that returns a personalized greeting as the response body. We use the `AddHeader` method to set an X-Greeting header with the same value as the response body. Overall, the go-restful package provides a powerful and easy-to-use API for building RESTful web services in Go.