私はgolangを初めて使っています。私はjulienschmidt/httprouterを使ってWebプロジェクトを作成しようとしています。私はよくフォーマットされた、よく構造化されたプロジェクトを作りたいと思っていますので、パフォーマンスやポインタを返すパフォーマンスについて2つの質問があります。私はそれを作成しているので、私の場合は
は私がリクエストからオブジェクトを返す関数を作成したい:Golang - 変数を渡して変数を返すベストプラクティス
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store, err := utilities.GetStoreFromRequest(r)
// other stuff
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request) (*models.Store, error) {
store := models.Store{}
err := json.NewDecoder(r.Body).Decode(&store)
// return a pointer is better than returning an object?
return &store, err
}
がそれを右ですかstoreControllerでストアオブジェクトを作成し、それを渡す方が良いです以下のような機能:
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store := models.Store{}
err := utilities.GetStoreFromRequest(r, &store)
// other stuff
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request, store *models.Store) error {
err := json.NewDecoder(r.Body).Decode(store)
return err
}
他の質問がポインタについてですが、代わりに常にオブジェクトおよびエラーのかいないのポインタを渡して、返すにはあまりにも偏執的ですか? ありがとう
[なぜ返信アドレスのコンストラクタを作成する必要がありますか?](http://stackoverflow.com/questions/31932822/why-should-constructor-of-go-return-address/31934189#31934189) [ポインタとパラメータの値と戻り値](http://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values)を参照してください。 – icza