结构命名约定
Struct Naming Convention
我在一个包中有几个结构,它们可以进行简单的财务 API 调用,一个实现对 API 的请求,一个存储响应。
我想知道将结构分别命名为 "Request" 和 "Response" 是否合适,或者我应该在它们前面加上 subject/package 前缀,例如 "FinanceRequest" 和"FinanceResponse"?还是使用 finance.FinanceRequest(金融词使用了两次)会使外部调用变得多余?
寻找关于此事的想法(golang convention/preference)...
样本:
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) {
//...
}
惯例是使用 Request 和 Response。这是 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.
golint tool will complain 关于名称 FinanceRequest 和 FinanceResponse。
我在一个包中有几个结构,它们可以进行简单的财务 API 调用,一个实现对 API 的请求,一个存储响应。
我想知道将结构分别命名为 "Request" 和 "Response" 是否合适,或者我应该在它们前面加上 subject/package 前缀,例如 "FinanceRequest" 和"FinanceResponse"?还是使用 finance.FinanceRequest(金融词使用了两次)会使外部调用变得多余?
寻找关于此事的想法(golang convention/preference)...
样本:
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) {
//...
}
惯例是使用 Request 和 Response。这是 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.
golint tool will complain 关于名称 FinanceRequest 和 FinanceResponse。