Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created November 26, 2016 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattetti/081201ff30642ce1a0644afcdd1938d4 to your computer and use it in GitHub Desktop.
Save mattetti/081201ff30642ce1a0644afcdd1938d4 to your computer and use it in GitHub Desktop.
example of using Go + Echo
/*
You need to have Go installed and make sure your $GOPATH is set.
Then, install echo by running:
go get -u github.com/labstack/echo
Finally run:
go run main.go
Where main.go is this file.
It will start the server on port 8080
You can now query your server using curl:
$ curl -i localhost:8080
$ curl -i -X POST localhost:8080
*/
package main
import (
"net/http"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.POST("/", postHandler)
e.GET("/", getHandler)
e.Logger.Fatal(e.Start(":8080"))
}
func getHandler(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func postHandler(c echo.Context) error {
return c.JSON(http.StatusOK, &postPayload{
Msg: "it worked",
Number: 42,
})
}
type postPayload struct {
Msg string `json:"message"`
Number int `json:"number"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment