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

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

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

问题

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

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

0

2

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

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

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

答案1

得分: 3

如果你尝试

  1. fmt.Println(header)

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

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

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

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

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

英文:

if you try

  1. fmt.Println(header)

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

  1. // HTTP defines that header names are case-insensitive.
  2. // The request parser implements this by canonicalizing the
  3. // name, making the first character and any characters
  4. // 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而不是字符串作为键。

  1. package main
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func main() {
  7. header := make(http.Header)
  8. var key = http.CanonicalHeaderKey("hello")
  9. header.Add(key, "world")
  10. header.Add(key, "anotherworld")
  11. fmt.Printf("%#v\n", header)
  12. fmt.Printf("%#v\n", header.Get(key))
  13. fmt.Printf("%#v\n", header[key])
  14. }

输出结果:

  1. http.Header{"Hello":[]string{"world", "anotherworld"}}
  2. "world"
  3. []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.

  1. package main
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func main() {
  7. header := make(http.Header)
  8. var key = http.CanonicalHeaderKey("hello")
  9. header.Add(key, "world")
  10. header.Add(key, "anotherworld")
  11. fmt.Printf("%#v\n", header)
  12. fmt.Printf("%#v\n", header.Get(key))
  13. fmt.Printf("%#v\n", header[key])
  14. }

The output:

  1. http.Header{"Hello":[]string{"world", "anotherworld"}}
  2. "world"
  3. []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:

确定