为什么http.Header中的切片长度返回为0?

huangapple go评论84阅读模式
英文:

Why the length of the slice in http.Header returns 0?

问题

从net/http的源代码中,http.Header的定义是map[string][]string。对吗?

但是为什么运行下面的代码,我得到的结果是:

0

2

func main() {
    var header = make(http.Header)
    header.Add("hello", "world")
    header.Add("hello", "anotherworld")
    var t = []string {"a", "b"}
    fmt.Printf("%d\n", len(header["hello"]))
    fmt.Print(len(t))
}
英文:

From the source code of net/http. The definition of http.Header is map[string][]string. Right?

But why go run below code, I get the result:

> 0
>
> 2

func main() {
    var header = make(http.Header)
    header.Add("hello", "world")
    header.Add("hello", "anotherworld")
    var t = []string {"a", "b"}
    fmt.Printf("%d\n", len(header["hello"]))
    fmt.Print(len(t))
}

答案1

得分: 3

如果你尝试

fmt.Println(header)

你会注意到键已经被大写了。这实际上在net/http的文档中有说明。

// HTTP定义标头名称不区分大小写。
// 请求解析器通过将名称规范化来实现这一点,
// 将连字符后的第一个字符和任何字符都大写,其余字符都小写。

这可以在类型为Request的Header字段的注释中找到。

http://golang.org/pkg/net/http/#Request

不过,注释可能应该被移动。

英文:

if you try

fmt.Println(header)

you'll notice that the key has been capitalized. This is actually noted in the documentation of net/http.

// HTTP defines that header names are case-insensitive.
// The request parser implements this by canonicalizing the
// name, making the first character and any characters
// following a hyphen uppercase and the rest lowercase.

This can be found in the comment on the field Header of type Request.

http://golang.org/pkg/net/http/#Request

Comment should probably be moved though..

答案2

得分: 3

看一下http.Header的参考和Get的代码:

> Get获取与给定键关联的第一个值。如果没有与该键关联的值,则Get返回“”。要访问一个键的多个值,请直接使用CanonicalHeaderKey访问映射。

因此,最好使用http.CanonicalHeaderKey而不是字符串作为键。

package main

import (
    "net/http"
    "fmt"
)

func main() {
    header := make(http.Header)
    var key = http.CanonicalHeaderKey("hello")

    header.Add(key, "world")
    header.Add(key, "anotherworld")

    fmt.Printf("%#v\n", header)
    fmt.Printf("%#v\n", header.Get(key))
    fmt.Printf("%#v\n", header[key])
}

输出结果:

http.Header{"Hello":[]string{"world", "anotherworld"}}
"world"
[]string{"world", "anotherworld"}
英文:

Take a look at the reference of http.Header and the code of Get:

> Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "". To access multiple values of a key, access the map directly with CanonicalHeaderKey.

So it helps to use http.CanonicalHeaderKey instead of strings for keys.

package main

import (
    "net/http"
    "fmt"
)

func main() {
    header := make(http.Header)
    var key = http.CanonicalHeaderKey("hello")

    header.Add(key, "world")
    header.Add(key, "anotherworld")

    fmt.Printf("%#v\n", header)
    fmt.Printf("%#v\n", header.Get(key))
    fmt.Printf("%#v\n", header[key])
}

The output:

http.Header{"Hello":[]string{"world", "anotherworld"}}
"world"
[]string{"world", "anotherworld"}

huangapple
  • 本文由 发表于 2012年9月25日 23:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/12586372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定