gpt4 book ai didi

dictionary - 为什么http.Header中slice的长度返回0?

转载 作者:IT王子 更新时间:2023-10-29 01:25:30 26 4
gpt4 key购买 nike

来自net/http的源代码。 http.Header的定义是map[string][]string。对吧?

但是为什么在代码下面go run,我得到了结果:

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

最佳答案

如果你尝试

fmt.Println(header)

您会注意到 key 已大写。这个其实在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.

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

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

不过应该​​可以移动评论..

关于dictionary - 为什么http.Header中slice的长度返回0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com