LoginSignup
1
2

More than 5 years have passed since last update.

【Go】EchoでHello Worldを表示する

Posted at

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を返すようにします。

スクリーンショット 2017-10-18 12.37.23.png
画面表示。

スクリーンショット 2017-10-18 12.36.34.png
curlでjson返却。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2