Created
November 16, 2009 16:17
-
-
Save fujiwara/236088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
. "http"; // http パッケージの関数を http. prefix なしで使えるように | |
"io"; | |
"fmt"; | |
) | |
func main() { | |
gourl := "http://golang.org/"; | |
Handle("/", HandlerFunc(func(c *Conn, req *Request) { | |
io.WriteString(c, "hello, world!\n"); | |
})); | |
Handle("/go", HandlerFunc(func(c *Conn, req *Request) { | |
// リダイレクト | |
Redirect(c, gourl, 302); | |
})); | |
Handle("/header", HandlerFunc(func(c *Conn, req *Request) { | |
// レスポンスヘッダを設定 | |
c.SetHeader("X-Powered-By", "Go <" + gourl + ">"); | |
io.WriteString(c, "ok"); | |
})); | |
Handle("/static/", HandlerFunc(func(c *Conn, req *Request) { | |
// ファイルを配信 | |
ServeFile(c, req, "." + req.URL.Path); | |
})); | |
Handle("/params", HandlerFunc(func(c *Conn, req *Request) { | |
body := ""; | |
for key, val := range req.Header { | |
// リクエストヘッダを列挙 | |
body = body + fmt.Sprintf("%s: %s<br>", key, val); | |
} | |
req.ParseForm(); // クエリパラメータを Parse | |
for key, val := range req.Form { | |
// クエリパラメータを列挙 | |
for i := range val { | |
body = body + fmt.Sprintf("%s=%s<br>", key, val[i]); | |
} | |
} | |
io.WriteString(c, body); | |
})); | |
err := ListenAndServe(":8080", nil); | |
if err != nil { | |
panic("ListenAndServe: ", err.String()) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment