LoginSignup
15
15

More than 5 years have passed since last update.

GoをインストールしてRevelを使ってみる

Posted at

Goを触ってて分からなかったこと・自信ないことで宣言したんですけど、今年はGo勉強します。
じゃあGoで何するのって、どうせ自分のことなんでWebアプリとかAPIサーバとかそんなのなんですよね。
ちうわけで、WebApplicationFramework使ってなんか作りたい。

使ってみるとかいいつつ、実際は何もしてません。インストールしてみただけです。

環境はUbuntuServer13.10の64bitです。

何はともあれGoをインストール

ビルドするのはめんどくさいので、ビルド済みのパッケージを持ってくることにします。
# aptでもインストールできるっぽいんですけど、1.1なので今回は避けました。

$ mkdir ~/work
$ cd ~/work
$ wget https://go.googlecode.com/files/go1.2.linux-amd64.tar.gz
$ tar zxvf go1.2.linux-amd64.tar.gz
$ mkdir ~/local
$ cp -r go ~/local
$ vi ~/.profile
$ source ~/.profile
~/.profile
# GOROOT、GOPATHは任意のところで大丈夫です。
# ただGOPATHは実行ユーザが権限が与えられているパスを指定してください。
export GOROOT=~/local/go
export GOPATH=~/local/_go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

ちゃんと入ってるか確認します。

hello.go
package main

import "fmt"

func main() {
  fmt.Printf("hello, world\n")
}
$ go run hello.go
hello, world

動いてます。

フレームワーク

[Go web framework]でググると、Revelが最初にヒットしたので、Revel使ってみます。
go getでパッケージをインストール出来るので、それでインストールします。

$ go get github.com/robfig/revel/revel
go: missing Mercurial command. See http://golang.org/s/gogetcmd
package github.com/robfig/revel/revel
        imports code.google.com/p/go.net/websocket: exec: "hg": executable file not found in $PATH

Woops! Mercurialが必要みたいです。場合によってはSubversionとかBazaarとかも入れる必要があります。
必要になったら入れるってことで、今は放置します。

$ sudo apt-get install mercurial
$ go get github.com/robfig/revel/revel
$ revel
~
~ revel! http://robfig.github.com/revel
~
usage: revel command [arguments]

The commands are:

    new         create a skeleton Revel application
    run         run a Revel application
    build       build a Revel application (e.g. for deployment)
    package     package a Revel application (e.g. for deployment)
    clean       clean a Revel application's temp files
    test        run all tests from the command-line

Use "revel help [command]" for more information.

フレームワークの準備ができた、のかな。

ひな形を作る

$ cd ~/work
$ mkdir revel-test
$ cd revel-test
$ revel new myapp
~
~ revel! http://robfig.github.com/revel
~
Your application is ready:
   /home/agen/local/_go/src/myapp

You can run it with:
   revel run myapp
$ ls
$

あれ、カレントディレクトリに作られないのこれ?
調べてみると、$GOPATH/src以下に新しいアプリケーションが作られていくみたいです。
で、今のところ任意の場所に作ることはできないみたいですね。

よし、わかった。とりあえずアクセスしてみる。

アクセスすると、おっきく「It works!」が。動いてるみたいです。

・・・さて、ここで一つ疑問が。
これどうバージョン管理すればいいんでしょう?
githubにあげるなら、revel new github.com/A-gen/go-blogとかでいいんですけど。。。
いや、じゃなくて、まあそれしか方法ないような気もするのですが、直接git initしていい、の?
形になったらpushして、自分でgo getとかするんでしょうか。

$ revel new github.com/A-gen/go-blog
~
~ revel! http://robfig.github.com/revel
~
Your application is ready:
   /home/agen/local/_go/src/github.com/A-gen/go-blog

You can run it with:
   revel run github.com/A-gen/go-blog
$ cd $GOPATH/src/github.com/A-gen/go-blog
$ git init
$ git add .
$ git commit -m "initial commit"

しばらくはこんな感じでやっていきます。

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