LoginSignup
0
0

More than 3 years have passed since last update.

go修行17日目 Webサーバ

Posted at

参考サイト

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

0
0
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
0
0