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

为啥初始化没有设置默认属性这些功能呢? #68

Closed
mengdu opened this issue Dec 22, 2020 · 1 comment
Closed

为啥初始化没有设置默认属性这些功能呢? #68

mengdu opened this issue Dec 22, 2020 · 1 comment

Comments

@mengdu
Copy link

mengdu commented Dec 22, 2020

使用时往往会有这样的场景,就是为当前模块创建一个独立的http client, 并且可以初始化一些默认参数
比方说:

defaultHeader := make(http.Header)
defaultHeader .Set("APPID", "xxxx")
r := req.New("https://api.github.com", defaultHeader)

res, _ := r.get("/users")
@imroc
Copy link
Owner

imroc commented Dec 22, 2020

req 的特点是轻量,这种场景实际可以自己稍稍封装个Client即可,示例:

package main

import (
	"github.com/imroc/req"
	"net/http"
)

type Client struct{
	*req.Req
	defaultHeaders http.Header
}

func NewClient(headers http.Header) *Client {
	return &Client{
		Req: req.New(),
		defaultHeaders: headers,
	}
}

func main() {
	defaultHeader := make(http.Header)
	defaultHeader .Set("APPID", "xxxx")
	c := NewClient(defaultHeader)
	res, _ := c.Get("http://xxx.com/users")
        ...
}

@imroc imroc closed this as completed Jan 28, 2022
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