GO结构填充:指针接收器与返回的值
从代码可读性/维护/最佳实践的角度来看,寻找参考良好的阅读或分享更好的体验。
关于如何将值分配给struct字段的两个选项:
- 调用函数/方法,该函数/方法返回值并在主代码中进行明确分配。
- 使用指针接收器方法并填充方法内的结构。
我并不是在这里真正关注这里的代码生产力,而是将来更多地关注代码的可读性和维护。第一种方法使主代码更加混乱,但允许从一个地方填充哪些字段。虽然第二个在需要跳入方法的同时使主要代码清洁器进行清洁,并查看在那里填写哪些确切字段的问题。
选项1-详细的主代码
type Customer struct {
ID int
FName string
LName string
DOB string
SacredNumber string
NotYet string
}
func (c *Customer) fillMyCustomerGeneralWReturn() (fname, lname, dob string) {
fname = getFNameByID(c.ID)
lname = getLNameByID(c.ID)
dob = getDOBByID(c.ID)
return fname, lname, dob
}
func (c *Customer) fillMyCustomerSacredInfoWReturn() string {
return getSacredNumberByIDFromVenus(c.ID)
}
func main() {
cust := Customer{ID: 5}
cust.FName, cust.LName, cust.DOB = cust.fillMyCustomerGeneralWReturn()
cust.SacredNumber = cust.fillMyCustomerSacredInfoWReturn()
}
选项2-清洁器主代码,但可见性较小
type Customer struct {
ID int
FName string
LName string
DOB string
SacredNumber string
NotYet string
}
func (c *Customer) fillMyCustomerGeneral() {
c.FName = getFNameByID(c.ID)
c.LName = getLNameByID(c.ID)
c.DOB = getDOBByID(c.ID)
}
func (c *Customer) fillMyCustomerSacredInfo() {
c.SacredNumber = getSacredNumberByIDFromVenus(c.ID)
}
func main() {
cust := Customer{ID: 5}
cust.fillMyCustomerGeneral()
cust.fillMyCustomerSacredInfo()
}
Looking for a reference to a good reading or sharing an experience of what is better from the code readability/maintenance/best-practice perspective.
There are two options on how to assign values to struct fields:
- call a function/method that returns values and do an explicit assignment in the main code
- use a pointer receiver method and fill the struct inside the method.
I'm not really concerned about the code productivity here, but more about code readability and maintenance in the future. The first method makes the main code more cluttered but allows to see which fields are filled from the one place. While the second one makes the main code cleaner while requiring to jump into methods and look there which exact fields are filled out. I wonder about the pros and cons of each approach and if are there any other ways to solve the readability problem.
Option 1 - detailed main code
type Customer struct {
ID int
FName string
LName string
DOB string
SacredNumber string
NotYet string
}
func (c *Customer) fillMyCustomerGeneralWReturn() (fname, lname, dob string) {
fname = getFNameByID(c.ID)
lname = getLNameByID(c.ID)
dob = getDOBByID(c.ID)
return fname, lname, dob
}
func (c *Customer) fillMyCustomerSacredInfoWReturn() string {
return getSacredNumberByIDFromVenus(c.ID)
}
func main() {
cust := Customer{ID: 5}
cust.FName, cust.LName, cust.DOB = cust.fillMyCustomerGeneralWReturn()
cust.SacredNumber = cust.fillMyCustomerSacredInfoWReturn()
}
Option 2 - cleaner main code, but less visibility
type Customer struct {
ID int
FName string
LName string
DOB string
SacredNumber string
NotYet string
}
func (c *Customer) fillMyCustomerGeneral() {
c.FName = getFNameByID(c.ID)
c.LName = getLNameByID(c.ID)
c.DOB = getDOBByID(c.ID)
}
func (c *Customer) fillMyCustomerSacredInfo() {
c.SacredNumber = getSacredNumberByIDFromVenus(c.ID)
}
func main() {
cust := Customer{ID: 5}
cust.fillMyCustomerGeneral()
cust.fillMyCustomerSacredInfo()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的建议。我想应该这样的事情:
Thanks for the suggestions. I suppose it should be something like this: