英文:
Golang - best practices to pass and return variables
问题
我正在尝试使用julienschmidt/httprouter创建一个Web项目。我希望创建一个格式良好、结构良好的项目,所以我有两个关于传递和返回值或指针性能的问题。
在我的情况下,我想创建一个从请求中返回对象的函数,所以我已经创建了它:
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store, err := utilities.GetStoreFromRequest(r)
// 其他操作
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request) (*models.Store, error) {
store := models.Store{}
err := json.NewDecoder(r.Body).Decode(&store)
// 返回指针比返回对象更好吗?
return &store, err
}
这样做对吗?还是更好的方法是在storeController中创建一个store对象,并将其传递给函数,像这样:
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store := models.Store{}
err := utilities.GetStoreFromRequest(r, &store)
// 其他操作
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request, store *models.Store) error {
err := json.NewDecoder(r.Body).Decode(store)
return err
}
另一个问题是关于指针,总是传递和返回指针而不是对象和错误是否太过谨慎?
英文:
I am trying to create a web project with the julienschmidt/httprouter. I am searching to create a well formatted and well structured project so I have two questions about performances passing and returning value or pointers.
In my case I want to create a function that from the request returns an object so I have created it:
// 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
}
Is it right or it is better to create a store object in the storeController and pass it to the function like:
// 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
}
The other question is about the pointer, is too paranoid to pass and return always pointers instead of object and errors or not?
答案1
得分: 1
通常情况下,消除无意义的参数是多余的且更好的做法。实际上,通过将其作为参数,它实际上被初始化为其零值。以下是所有有效的方法:
func GetStoreFromRequest(r *http.Request) (store *models.Store, err error) {
err = json.NewDecoder(r.Body).Decode(store)
return
}
func GetStoreFromRequest(r *http.Request, store *models.Store) error {
err := json.NewDecoder(r.Body).Decode(store)
return err
}
func GetStoreFromRequest(r *http.Request) (*models.Store, error) {
var store models.Store // 或者 store := models.Store{}
err = json.NewDecoder(r.Body).Decode(&store)
return &store, err
}
通常最佳实践是将局部变量保持在局部范围内--想象一下将参数 i 传递给 for 循环来使用。这没有太多意义,对吧?因此,在这种情况下,我建议选择选项 1 或 3(它们本质上是相同的执行),并将局部变量从函数签名中省略掉。
英文:
It's usually redundant and better to eliminate pointless parameters. In fact, by having it as a parameter it's actually initialized to its nil value. Here are all valid ways to do this:
func GetStoreFromRequest(r *http.Request) (store *models.Store, err error) {
err = json.NewDecoder(r.Body).Decode(store)
return
}
func GetStoreFromRequest(r *http.Request, store *models.Store) error {
err := json.NewDecoder(r.Body).Decode(store)
return err
}
func GetStoreFromRequest(r *http.Request) (*models.Store, error) {
var store models.Store // or store := models.Store{}
err = json.NewDecoder(r.Body).Decode(&store)
return &store, err
}
It's usually best practice to keep local variables local--imagine passing a parameter i to be used in a for loop. Doesn't make much sense, right? So for this situation, I'd recommend options 1 or 3 (which are essentially the same execution) and leave the local variable out of the function signature.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论