2016-02-26 48 views
3

我有一個包幾結構,使一個簡單的財務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) { 
//... 
} 

回答

4

約定是使用請求和響應。下面是Effective Go不得不說:

一包的進口商將使用名稱來指代的內容,所以出口名稱在包裝可以用這個事實來避免口吃。 (不要使用導入。記號,這樣可以簡化必須他們正在測試包的外部運行測試,但原本應該是可以避免的。)例如,在BUFIO包緩衝的讀者類型被稱爲讀者,而不是BufReader,因爲用戶將其視爲bufio.Reader,這是一個清晰,簡潔的名稱。而且,因爲導入的實體總是以其包名稱尋址,所以bufio.Reader不會與io.Reader衝突。

golint工具will complain關於名稱FinanceRequest和FinanceResponse。

+0

謝謝,這正是我一直在尋找! – Christopher