Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created November 16, 2009 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/236088 to your computer and use it in GitHub Desktop.
Save fujiwara/236088 to your computer and use it in GitHub Desktop.
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