Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否支持SetHeader多次使用呢,现在使用起来看起来只能使用一次 #203

Closed
chanyipiaomiao opened this issue Mar 26, 2020 · 6 comments

Comments

@chanyipiaomiao
Copy link

能否支持SetHeader多次使用呢,现在使用起来看起来只能使用一次

@guonaihong
Copy link
Owner

guonaihong commented Mar 27, 2020

现状

SetHeader目前是只能调用一次,多次调用后面的会覆盖前面。如果要传递多个header,只要传递slice or map or struct类等的数据就行更多SetHeader用法例子

func arrayExample() {
	// 2.使用slice变量
	fmt.Printf("======2. SetHeader======use slice=====\n")
	err := gout.GET(":8080/test.header").
		Debug(true).
		SetHeader(gout.A{"h1", "v1",
			"h2", 2,
			"h3", float32(3.14),
			"h4", 4.56,
			"h5", time.Now().Unix(),
			"h6", time.Now().UnixNano(),
			"h7", time.Now().Format("2006-01-02")}).
		Do()
	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}
}

后续

你提的我会考虑下,不过目前优先级不会太高。支持多次调用要的效果,在目前单次调用SetHeader里面都可以实现。

以前不支持多次调用SetHeader的考虑有。

  • 如果要传递多个header,只调用一次SetHeader(只传递string slice)和多次调用SetHeader的设计其实是一样的(每次传string)。
  • 只支持一次SetHeader的次数更好预估,内存分配大小更好预算,效率相对会好点。

@chanyipiaomiao
Copy link
Author

好的,有时候多个业务需要一个公用的头部,每个业务可能有不同的头部,先设置公用的头部之后,再分别各自的头部,不过这都不是问题,可以设计一下就好了。

@guonaihong
Copy link
Owner

guonaihong commented Apr 1, 2020

hi, @chanyipiaomiao 实现你的需求,现在有两套方案,我们可以讨论下。

  • 第一个是可变长参数
SetHeader(结构体1结构体2)
  • 第二个是多次调用SetHeader
SetHeader(结构体1).SetHeader( 结构体2)

目前考虑

目前倾向于第一种方案,更符合go的风格,也是整个go标准库惯用的方式。第二种方案中用多次SetHeader实现的却是append的语义,实现这样的行为感觉怪怪的。

@chanyipiaomiao
Copy link
Author

可以的。

@guonaihong
Copy link
Owner

ok,那就第一种。

@guonaihong
Copy link
Owner

功能已经提交到master分支,可以体验下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants