Created
November 26, 2016 16:56
-
-
Save mattetti/081201ff30642ce1a0644afcdd1938d4 to your computer and use it in GitHub Desktop.
example of using Go + Echo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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