GO结构填充:指针接收器与返回的值

发布于 2025-01-25 18:43:59 字数 1568 浏览 2 评论 0原文

从代码可读性/维护/最佳实践的角度来看,寻找参考良好的阅读或分享更好的体验。

关于如何将值分配给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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-02-01 18:43:59

感谢您的建议。我想应该这样的事情:

type Customer struct {
    ID           int
    FName        string
    LName        string
    DOB          string
    SacredNumber string
    NotYet       string
}

type CustomerRepository struct {
}

func (*CustomerRepository) FindByID(c int) *Customer {
    var returnCustomer Customer
    returnCustomer.ID = c
    returnCustomer.FName = getFNameByID(c)
    returnCustomer.LName = getLNameByID(c)
    returnCustomer.DOB = getDOBByID(c)
    return &returnCustomer
}

func (*CustomerRepository) SetSacredNumber(c *Customer) {
    c.SacredNumber = getSacredNumberByIDFromVenus(c.ID)
    return
}

func main() {

    var customerRepo CustomerRepository
    cust := customerRepo.FindByID(5)
    customerRepo.SetSacredNumber(cust)
    fmt.Println(*cust)
}

Thanks for the suggestions. I suppose it should be something like this:

type Customer struct {
    ID           int
    FName        string
    LName        string
    DOB          string
    SacredNumber string
    NotYet       string
}

type CustomerRepository struct {
}

func (*CustomerRepository) FindByID(c int) *Customer {
    var returnCustomer Customer
    returnCustomer.ID = c
    returnCustomer.FName = getFNameByID(c)
    returnCustomer.LName = getLNameByID(c)
    returnCustomer.DOB = getDOBByID(c)
    return &returnCustomer
}

func (*CustomerRepository) SetSacredNumber(c *Customer) {
    c.SacredNumber = getSacredNumberByIDFromVenus(c.ID)
    return
}

func main() {

    var customerRepo CustomerRepository
    cust := customerRepo.FindByID(5)
    customerRepo.SetSacredNumber(cust)
    fmt.Println(*cust)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文