9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 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 等と打てば動作確認できると思います。

9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?