LoginSignup
3
3

More than 5 years have passed since last update.

GAE/Gの"Hello, World!"をHTTPサーバーに書き換える手順

Posted at

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!")
}
3
3
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
3
3