Help us understand the problem. What is going on with this article?

gomでginをrunする

More than 3 years have passed since last update.

gomはgoのパッケージをgemっぽく管理できるやつ

プロジェクトのカレントディレクトに行き、まずgomを入れる。

$ go get github.com/mattn/gom

んでGomfileをつくる。

$ gom gen gomfile

ファイルの中身はこんな感じ

$ gom 'github.com/gin-gonic/gin' 

パッケージは予めinstallする必要があります

$ gom install

こうすると_vendorディレクトにginがインストールされます。

で、main.goをとりあえずginのサンプルどおり作ってみます。

main.go
func main() {
    router := gin.Default()

    // This handler will match /user/john but will not match neither /user/ or /user
    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    // However, this one will match /user/john/ and also /user/john/send
    // If no other routers match /user/john, it will redirect to /user/join/
    router.GET("/user/:name/*action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        message := name + " is " + action
        c.String(http.StatusOK, message)
    })

    router.Run(":8080")
}

それでは実行してみましょう。_vendorディレクトを有効にするためにgomコマンドを使って実行します。

$ gom run main.go
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET   /user/:name               --> main.main.func1 (3 handlers)
[GIN-debug] GET   /user/:name/*action       --> main.main.func2 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080

こんな感じで実行されブラウザから http://localhost:8080/user/oresama 等と打てば動作確認できると思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした