GoのフレームワークEchoを使ってHello Worldを表示してみる。
echoはインストールされている前提です。
//main.go
package main
import (
"github.com/labstack/echo"
"./handler"
)
func main() {
// Echoのインスタンス作る
e := echo.New()
// ルーティング
e.GET("/hello", handler.MainPage())
e.GET("/api/hello", handler.ApiHelloGet())
// サーバー起動
e.Start(":8080")
}
package handler
import (
"net/http"
"github.com/labstack/echo"
)
func MainPage() echo.HandlerFunc {
return func(c echo.Context) error {
return c.String(http.StatusOK, "Hello World")
}
}
func ApiHelloGet() echo.HandlerFunc {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]interface{}{"hello": "world"})
}
}
/api/helloのルーティングはjsonを返すようにします。