株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

参考サイト

https://echo.labstack.com/guide
https://rightcode.co.jp/blog/information-technology/golang-introduction-rest-api

パッケージインストール

go get -u github.com/labstack/echo/...

コード

  • localhost:1323で起動する
package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}
PS C:\Users\yuta\go\src\server> go run .\server.go

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.16
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:1323

image.png

ユーザー名を返す

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.GET("/users/:name", getUserName)
    e.Logger.Fatal(e.Start(":1323"))

}

func getUserName(c echo.Context) error {
    name := c.Param("name")
    return c.String(http.StatusOK, name)
}

image.png

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)