golang中访问GET参数net / http的问题

Following is my go program to extract GET parameters. (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))
}

Error shown on console.

"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)

I am having trouble with determining the data type of hostname variable. I should get it as a string.

When I try to write to http response writer

w.Write([]byte(hostname))

following build error is shown

go/src/api/main.go:38: cannot convert hostname (type []string) to type []byte

If I treat it as a string array build is successful but there is run time error

        newhost := hostname[0];
        w.Write([]byte(newhost))

Error:

    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)

I also checked length of newhost as @VonC suggested and it gave expected results.

        fmt.Print("
")
        fmt.Println((len(newhost)))

Output on console

"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)

You need to use url/Values.Get, it will return the empty string if the value isn't set:

hostname := r.URL.Query().Get("hostname")
if len(hostname) != 0 {
    io.WriteString(w, hostname)  // or
    w.Write([]byte(hostname))
}

// edit

Let's take it a step by step:

go/src/api/main.go:38: cannot convert hostname (type []string) to type []byte:

  • req.URL.Query["hostname"], returns a string slice ([]string) which you can't directly convert to []byte, only string can be converted like that.

The first runtime error: index out of range:

  • You're trying to use hostname[0] without checking how many elements in the slice, so in that case there weren't any elements.

That last runtime error: index out of range is weird though, I have no idea why you got it, must be something else you changed in the code.

If you only care about the first value, and you don't have to distinguish between POST or GET, you might consider using the simpler Request.FormValue function.

hostname := r.FormValue("hostname")