In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use the GO functional option model, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
One of the many problems encountered by Golang developers is trying to set the parameters of a function to optional. This is a very common use case, some objects should be used out of the box with some basic default settings, and you may occasionally need to provide some more detailed configuration.
This is easy in many languages; in C languages, you can provide multiple versions of the same function with a different number of arguments; in a language like PHP, you can give parameters a default value and ignore them when calling methods. But in Golang, you can't use either of these methods. So how do you create a function where the user can specify some additional configuration?
There are many possible ways to do this, but most of them don't meet the requirements, or require additional checking and validation in the server's code, or do extra work for the client by passing additional parameters that the client doesn't care about.
The GO functional option mode (Functional Options Pattern) is introduced below, which is as follows:
Advantages and disadvantages of Option model
Advantages:
1. Support passing multiple parameters, and maintain compatibility when the number and type of parameters change
two。 Transfer parameters in any order
3. Default values are supported
4. Convenient for expansion
Disadvantages:
1. Increase the cost by adding a lot of function
two。 When the parameters are not too complex, use them as little as possible
DEMO
The arguments to the package mainimport "fmt" type Client struct {Id int64 AppKey string AppSecret string} type Option func (* Client) / / go function are passed by value, so if you want to modify Client (default), you must pass the pointer func WithAppKey (appKey string) Option {return func (client * Client) {client.AppKey = appKey}} func WithAppSecret (appSecret string) Option { Return func (client * Client) {client.AppSecret = appSecret}} / NewClient// @ Description sets the parameter of a function to the optional function / / @ param id fixed parameter You can also put everything in the optional parameter opts / / @ param opts// @ return Client return * Client and Client can both / / func NewClient (id int64, opts... Option) Client {o: = Client {Id: id, AppKey: "key_123456", AppSecret: "secret_123456",} for _ Opt: = range opts {opt (& o) / / the parameters of the go function are passed by value, so if you want to modify Client (default), you must pass a pointer} return o} func main () {/ / use the default value fmt.Println (NewClient (1)) / / {1 key_123456 secret_123456} / / use pass The incoming value fmt.Println (NewClient (2) WithAppKey ("change_key_222")) / / {2 change_key_222 secret_123456} / / pass fmt.Println (NewClient (3, WithAppSecret ("change_secret_333")) / / {3 key_123456 change_secret_333} fmt.Println (NewClient (4, WithAppSecret ("change_secret_444")) out of order WithAppKey ("change_key_444")) / / {4 change_key_444 change_secret_444}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.