Google App Engine for Go(GAE/G)の"Hello, world!"をHTTPサーバーに書き換える3つの手順+α。
1.package名を「main」に変更。
2.init()関数名を「main()」に変更。
3.名称変更したmain()関数の最後に、リクエストを待ち受けるための一行を追加。
// 動作しない人はメソッド名の最後に"r"が付いていないか確認!
http.ListenAndServe(":8080", nil)
ListenAndServe()関数にて、ポート番号を":80"で待ち受けたい場合はroot権限で起動する必要があります。
sudo go run hello.go
HTTPサーバー用に書き換えたソース
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}