LoginSignup
6
7

More than 5 years have passed since last update.

Revel(Golang)でViewを理解する

Posted at

Revelとは

RailsっぽいGoのWebFrameworkです。
遅い重いとは言われていますが、思想を統一するというのもチーム開発で大事だと思うので、
そこのコストを削減するために導入はありだと思います。

Revelの構成

revelでプロジェクトを作るとREADME.mdが勝手に作られてそこに色々説明が書いてあります。

Description of Contents

The default directory structure of a generated Revel application:

myapp               App root
  app               App sources
    controllers     App controllers
      init.go       Interceptor registration
    models          App domain models
    routes          Reverse routes (generated code)  # also in frontend source
    views           Templates
  tests             Test suites
  conf              Configuration files
    app.conf        Main configuration file
    routes          Routes definition
  messages          Message files
  public            Public assets # also in frontend source
    css             CSS files
    js              Javascript files
    images          Image files

Controllerを用意する

こんな感じでControllerを追加してあげます。
スクリーンショット 2016-02-24 0.28.03.png

// top.go
package controllers

import "github.com/revel/revel"

type Top struct {
    *revel.Controller
}

func (h Top) Index() revel.Result {
    return h.Render()
}

Viewを用意する

上記の下のメソッドが重要でGoだと命名規則がとても大事で、Indexという名前をつけると以下のように用意しておいたViewのtemplateが呼び出されます。

スクリーンショット 2016-02-24 0.31.31.png

Routeをいじる

myapp/conf/route.confに以下のように書くと
localhost:9000でtop.htmlが表示されるようになります。

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

module:testrunner

# top
GET     /                                       Top.Index

Revelのbookingのサンプルが一番わかりやすいのでぜひ見てみてください。
https://revel.github.io/samples/booking.html

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