Golang return 使用 New() 构造指针而不是直接创建一个

Golang return struct pointer with New() instead of creating one directly

我正在阅读这个 repo 单元测试代码,Client 结构是以我以前从未见过的方式创建的。

type Client struct {
    // client stuff
}

// In client_test.go
// Creating default client for testing
c := dc()

// In resty_test.go
func dc() *Client {
    DefaultClient = New()
    DefaultClient.SetLogger(ioutil.Discard)
    return DefaultClient
}

我的问题是返回New()的目的是什么? 下面的代码是否与 New() 风格类似?为什么要二选一?

func dc() *Client {
    DefaultClient := Client{}
    return &DefaultClient
}

New() 函数是 Client:

的构造函数

https://github.com/go-resty/resty/blob/63ac6744519b3b3e976256d87d7b097c3a2c8dbc/default.go#L25

使用构造函数允许使用设置的默认值构造结构,而不是像 Client{} 那样对所有内部字段使用零值。例如,在这种情况下,最大正文大小设置为 math.MaxInt32 而不是 0。