LoginSignup
59
58

More than 5 years have passed since last update.

Golang開発環境 3分クッキング

Last updated at Posted at 2015-07-29

あと3分でGolangの環境が欲しい

そういうこと、よくありますよね。
はい、ではこちらにあらかじめ茹でておいた CentOS 6.5 の環境があります。

$ cat /etc/redhat-release 
CentOS release 6.5 (Final)

0:00 Golangのインストール

$ cd /usr/local/src
$ wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
$ tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz

0:29 ~/.bashrcにGOPATHを追記

$ vim ~/.bashrc
~/.bashrc
export GOPATH=/usr/local/go/
export PATH=$PATH:/usr/local/go/bin
$ source ~/.bashrc

0:48 freshをインストール

https://github.com/pilu/fresh
ディレクトリ監視 & 自動ビルド & 実行ツール

$ go get github.com/pilu/fresh

1:34 NeoBundleのダウンロード

$ mkdir -p ~/.vim/bundle
$ git clone git://github.com/Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim

1:48 ~/.vimrcに以下を追記してvim-goをインストール

https://github.com/fatih/vim-go
ここでvim-goのエラー通知を止めているので、freshのログからエラーを読み取ることになります。
また保存後に行われるオートフォーマットも止めています。
こちらをONにしているとアンドゥが効かなくなります。

$ vim ~/.vimrc
~/.vimrc
set nocompatible
filetype off

if has('vim_starting')
  set runtimepath+=~/.vim/bundle/neobundle.vim/
endif

filetype plugin indent on

call neobundle#begin(expand('~/.vim/bundle/'))
NeoBundleFetch 'Shougo/neobundle.vim'
NeoBundle 'fatih/vim-go'
call neobundle#end()

let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
let g:go_fmt_fail_silently = 1
let g:go_fmt_autosave = 0
syntax on

2:05 適当なディレクトリを作りVimを起動

$ mkdir /usr/local/bin/project
$ cd /usr/local/bin/project
$ vim server.go

2:19 Vim上で :NeoBundleInstall

:NeoBundleInstall

2:37 以下のコードをコピペ

急げ間に合わない。

server.go
package main

import (
        "fmt"
        "net/http"
)

func main() {
        http.HandleFunc("/", root)
        http.ListenAndServe(":8080", nil)
}

func root(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello World.")
}

2:46 Vimを閉じてfreshを起動

$ fresh

fresh.png

3:00 「サーバのIPアドレス:8080」にブラウザからアクセス

http://localhost:8080
Hello World. と表示されたら完了。

ところでGolangってなに?

  • 非常に単純な構文の言語です
  • 公式チュートリアルをこなせば大体覚えられる手軽さ
  • 並列処理が言語レベルでサポートされていて、割と楽に実装できます(switch-select ンギモチイイ)
  • 今のところ存在するORMがちょっとキモい

Webフレームワークあんの?

ありますが、MVCの形は自分で作るもの。

Goji (Routing & Controller)

https://github.com/zenazn/goji

Genmai (Model)

https://github.com/naoina/genmai

template (View)

http://blog.inagaki.in/?p=212

3分まで縮めるのに何回練習したの?

お前は今までに打ったvagrant destroyの数を覚えているのか。
普通にやると5分くらいかかります。

参考文献

http://qiita.com/hit/items/875259a27079aff14f98
http://qiita.com/izumin5210/items/1f3c312edd7f0075b09c
http://qiita.com/Kuchitama/items/68b6b5d5ed40f6f96310

59
58
3

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
59
58