以下是我编写的Go程序,用于提取GET参数。(URL:/mysql?hostname=example.com)
package main
import (
"net/http"
"fmt"
//"encoding/json"
//"html"
"github.com/kr/pretty"
);
func main(){
http.HandleFunc("/", foo)
http.ListenAndServe(":80", nil)
}
func foo(w http.ResponseWriter, r * http.Request){
w.Header().Set("Server","A Go WebServer")
w.Header().Set("Content-Type", "text/html")
hostname := r.URL.Query()["hostname"]
//w.Write([]byte(hostname[0]))
fmt.Printf("%# v", pretty.Formatter(hostname[0]))
w.Write([]byte(hostname))
}
在控制台上显示的错误。
"example.com"2014/09/01 02:57:22 http: panic serving 172.17.92.14:49411: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42dc41)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f8b5)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410381)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x410228)
/root/go/src/api/main.go:34 (0x400d77)
foo: fmt.Printf("%# v", pretty.Formatter(hostname[0]))
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421bed)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422a6f)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x421a00)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d9bb)
2014/09/01 02:57:22 http: panic serving 172.17.92.14:49412: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42dc41)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f8b5)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410381)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x410228)
/root/go/src/api/main.go:34 (0x400d77)
foo: fmt.Printf("%# v", pretty.Formatter(hostname[0]))
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421bed)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422a6f)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x421a00)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d9bb)
2014/09/01 02:57:22 http: panic serving 172.17.92.14:49413: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42dc41)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f8b5)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410381)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x410228)
/root/go/src/api/main.go:34 (0x400d77)
foo: fmt.Printf("%# v", pretty.Formatter(hostname[0]))
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421bed)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422a6f)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x421a00)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d9bb)
我在确定主机名变量的数据类型方面遇到了麻烦,我应该将其作为字符串获取。
当我尝试写入 HTTP 响应写入程序时
w.Write([]byte(hostname))
显示以下构建错误
go/src/api/main.go:38: cannot convert hostname (type []string) to type []byte
如果我将其视为字符串数组,则构建成功,但在运行时会出现错误。
newhost := hostname[0];
w.Write([]byte(newhost))
错误:
2014/09/01 04:42:40 http: panic serving 172.17.92.14:50404: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42db27)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f79b)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410267)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x41010e)
/root/go/src/api/main.go:38 (0x400d63)
foo: newhost := hostname[0];
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421ad3)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422955)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x4218e6)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d8a1)
2014/09/01 04:42:40 http: panic serving 172.17.92.14:50405: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42db27)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f79b)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410267)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x41010e)
/root/go/src/api/main.go:38 (0x400d63)
foo: newhost := hostname[0];
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421ad3)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422955)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x4218e6)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d8a1)
2014/09/01 04:42:40 http: panic serving 172.17.92.14:50406: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42db27)
_func_003: buf.Write(debug.Stack())
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:1443 (0x40f79b)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:128 (0x410267)
/home/michael/DPKG/golang/src/pkg/runtime/runtime.c:85 (0x41010e)
/root/go/src/api/main.go:38 (0x400d63)
foo: newhost := hostname[0];
/usr/lib/go/src/pkg/net/http/server.go:690 (0x421ad3)
HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:926 (0x422955)
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/lib/go/src/pkg/net/http/server.go:656 (0x4218e6)
(*conn).serve: handler.ServeHTTP(w, w.req)
/home/michael/DPKG/golang/src/pkg/runtime/proc.c:271 (0x40d8a1)
正如@VonC建议的那样,我还检查了newhost的长度,并得到了预期的结果。
fmt.Print("\n")
fmt.Println((len(newhost)))
控制台输出
"example.com"
11
2014/09/01 05:05:15 http: panic serving 172.17.92.14:50779: runtime error: index out of range
/usr/lib/go/src/pkg/net/http/server.go:576 (0x42dd33)
go version
打印的是什么内容? - OneOfOne