web-services - 在 golang 中访问 GET 参数 net/http 的问题

标签 web-services http go

以下是我提取 GET 参数的 go 程序。 (网址:/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 的建议检查了新主机的长度,它给出了预期的结果。

        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)

最佳答案

您需要使用 url/Values.Get ,如果未设置值,它将返回空字符串:

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

//编辑

让我们一步一步来:

go/src/api/main.go:38: 无法将主机名(类型 []string)转换为类型 []byte:

  • req.URL.Query["hostname"],返回一个不能直接转换成[]的字符串 slice ([]string) byte,只有string可以这样转换。

第一个运行时错误:索引超出范围:

  • 您尝试使用 hostname[0] 而不检查 slice 中有多少元素,因此在这种情况下没有任何元素。

最后一个 runtime error: index out of range 很奇怪,我不知道你为什么得到它,一定是你在代码中更改的其他内容。

关于web-services - 在 golang 中访问 GET 参数 net/http 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25600521/

相关文章:

objective-c - 使用二进制图像数据更新 CakePHP Web 服务

java - 通过 JSP 使用 Web 服务 JAX

Linux TCP 在重负载下奇怪地没有响应

api - 如果第 3 方 API 身份验证失败,我的 API 应该返回哪些 HTTP 错误代码?

file - 打开文件并在另一个函数中创建 NewReader 后如何关闭文件?

java - 访问网络服务

web-services - 如何为 Web 服务声明复杂的嵌套 C# 类型

http - 获取没有内容长度的 http 请求的响应?

go - 为什么 Go 支持 Plan9?

go - bytes.String() 与 bytes.Bytes() 在 Go 中