How to Implement a Common `Save()` Method for Go Structs Sharing a Field?

Susan Sarandon
Release: 2024-11-24 07:03:11
Original
296 people have browsed it

How to Implement a Common `Save()` Method for Go Structs Sharing a Field?

How to Implement a Common Method for Structs with a Shared Field in Go

When working with structs that share a common field, it may be desirable to add a method that can be applied to all of them. This question explores this scenario in the context of Beego/ORM, where two structs, ModelA and ModelB, need a Save() method.

Proposed Solutions

  • Interface Definition: Define an interface called Savable that declares the Save() method. Implement this interface for both ModelA and ModelB to allow them to utilize the Save() functionality.

<br>type Savable interface {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">Save()
Copy after login

}

func (a ModelA) Save() {

// Implementation for ModelA
Copy after login

}

func (b ModelB) Save() {

// Implementation for ModelB
Copy after login

}

  • Embedding: Create a base struct called ModelC that contains the shared Guid field. Embed this struct into ModelA and ModelB. Define the Save() method in ModelC to make it available to both ModelA and ModelB.

<br>type ModelC struct {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">Guid string `orm:"pk"`
Copy after login

}

func (c ModelC) Save() error {

// Implementation for ModelC
Copy after login

}

type ModelA struct {

ModelC
FiledA string
Copy after login

}

type ModelB struct {

ModelC
FiledB string
Copy after login

}

Caution with Embedding

It should be noted that embedding has certain limitations. While the embedded Save() method will be available to ModelA and ModelB, any additional fields specific to these structs will not be automatically included in the Save() operation.

Conclusion

The most appropriate solution depends on the specific requirements of the system. If the Save() implementation varies significantly between ModelA and ModelB, the interface approach provides greater flexibility. However, if they share a common implementation, embedding may be more efficient since it eliminates the need for redundant code.

The above is the detailed content of How to Implement a Common `Save()` Method for Go Structs Sharing a Field?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Previous article:How Can I Efficiently Access Random Runes in Go Strings Without Using For Loops? Next article:How to Safely Retrieve Values from a `map[string]interface{}` in Go?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template