Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: flags: <Type>VarDefault functions #68714

Open
dimovnike opened this issue Aug 2, 2024 · 3 comments
Open

proposal: flags: <Type>VarDefault functions #68714

dimovnike opened this issue Aug 2, 2024 · 3 comments
Labels
Milestone

Comments

@dimovnike
Copy link

dimovnike commented Aug 2, 2024

Proposal Details

Implement flag.VarDefault() functions that would take the default value from the variable itself. For example:

var p string = "default value"
flag.StringVarDefault(&p,"flagName", "usage")

should equal to:

var p string = "default value"
flag.StringVar(&p,"flagName", p, "usage")

If approved, I would like to implement this myself.

@gopherbot gopherbot added this to the Proposal milestone Aug 2, 2024
@gabyhelp
Copy link

gabyhelp commented Aug 2, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

This would be a massive expansion of api surface for almost no / negative gain in clarity and length of code.

We'd need to see more concrete justification for this proposal.

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 2, 2024
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 2, 2024
@dimovnike
Copy link
Author

dimovnike commented Aug 5, 2024

In my usage I expose structure fields as flags. The structures are offered by external packages and have default values. Here is an example:

// get the config with default values
cfg := package1.NewConfig()

// expose some fields as flags
flag.DurationVar(&cfg.Field1, "config-field1", cfg.Field1, "description")
flag.DurationVar(&cfg.Field2, "config-field2", cfg.Field2, "description")

The problem is that current implementation is prone to copy-paste errors.

Here is a real example: https://github.com/dimovnike/go-jwks-server/blob/master/internal/config/config.go

@seankhliao seankhliao removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 5, 2024
@seankhliao seankhliao changed the title proposal: flags: Implement flag.<Type>VarDefault functions proposal: flags: <Type>VarDefault functions Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants