GO言語をちょっといじった過程のめも
(1) yumからインストール
# yum install epel-release # yum install golang
参照元:Installing Go Lang 1.1 on centos 6.4 *64-bit ~ Naresh
(2) 必須環境変数を設定
- PATHはGOPATHを追加。
- それ以外は行追加。
- GOROOTはyumからインストールした際に必要となる。
# cd ~ # vim .bash_profile
GOROOT=/usr/lib/golang GOPATH=$HOME/go PATH=$PATH:$HOME/bin:$GOPATH/bin export GOROOT export GOPATH
# source .bash_profile
参照元:
Goコードの書き方 - The Go Programming Language
(3) テスト1 hello world
# vim hello.go
package main import ( "fmt" ) func main() { fmt.Printf("Hello, world.\n") }
# go run hello.go
参照元:
Goコードの書き方 - The Go Programming Language
(4) テスト2 簡易htmlサーバ
# vim server.go
package main import ( "fmt" "net/http" ) var str_agent []string func handler(w http.ResponseWriter, r *http.Request) { str_agent = append(str_agent, r.Header["User-Agent"][0]) fmt.Fprintf(w, "<html><body>\n") for i, s := range str_agent { fmt.Fprintf(w, "%d: %s<br>\n", i, s) } fmt.Fprintf(w, "</body></html>\n") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
# go run server.go
ブラウザで http://設置サーバのURL:8080 を参照すると、user agentが表示される。
こんなんでサーバ立ち上がっちゃうの!ってちょっとびっくり。
参照元:
Go言語でhttpサーバーを立ち上げてHello Worldをする - Qiita