gpt4 book ai didi

go - 结构命名约定

转载 作者:IT王子 更新时间:2023-10-29 00:39:21 24 4
gpt4 key购买 nike

我在一个包中有几个结构可以进行简单的金融 API 调用,一个实现对 API 的请求,一个存储响应。

我想知道将结构分别命名为“Request”和“Response”是否合适,或者我是否应该在它们前面加上主题/包前缀,例如“FinanceRequest”和“FinanceResponse”?还是使用 finance.FinanceRequest(财务词用了两次)会使外部调用变得多余?

寻找关于此事的想法(golang 约定/偏好)...

示例:

package finance

type Request struct {
//...
}

type Response struct {
//...
}

func DoSomething(r *Request) (*Response, error) {
//...
}

package finance

type FinanceRequest struct {
//...
}

type FinanceResponse struct {
//...
}

func DoSomething(r *FinanceRequest) (*FinanceResponse, error) {
//...
}

最佳答案

惯例是使用请求和响应。这是 Effective Go 必须说的:

The importer of a package will use the name to refer to its contents, so exported names in the package can use that fact to avoid stutter. (Don't use the import . notation, which can simplify tests that must run outside the package they are testing, but should otherwise be avoided.) For instance, the buffered reader type in the bufio package is called Reader, not BufReader, because users see it as bufio.Reader, which is a clear, concise name. Moreover, because imported entities are always addressed with their package name, bufio.Reader does not conflict with io.Reader.

关于名称 FinanceRequest 和 FinanceResponse 的 golint 工具 will complain

关于go - 结构命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35662673/

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