doujie4050 2014-09-01 07:09
浏览 107
已采纳

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)
  • 写回答

2条回答 默认 最新

  • duanfei8399 2014-09-01 13:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?