-
Notifications
You must be signed in to change notification settings - Fork 273
Add support for optional arguments #538
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
+905
−20
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cbeefc
add support for optional args
d0d774f
add files
natefinch 119a93c
add support for bare optional bool flags
natefinch a40dfd9
remove temp code
natefinch a4e22d8
update docs
natefinch 46e5965
fix some formatting
natefinch 001e182
more formatting
natefinch 96c3d87
add min version for optional args in docs
natefinch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //go:build mage | ||
| // +build mage | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // Greet greets someone with an optional greeting. | ||
| func Greet(name string, greeting *string) { | ||
| if greeting != nil { | ||
| fmt.Printf("%s, %s!\n", *greeting, name) | ||
| } else { | ||
| fmt.Printf("Hello, %s!\n", name) | ||
| } | ||
| } | ||
|
|
||
| // Add adds two numbers. The second number is optional and defaults to 0. | ||
| func Add(a int, b *int) { | ||
| if b != nil { | ||
| fmt.Println(a + *b) | ||
| } else { | ||
| fmt.Println(a) | ||
| } | ||
| } | ||
|
|
||
| // Scale scales a value by an optional factor. | ||
| func Scale(value float64, factor *float64) { | ||
| if factor != nil { | ||
| fmt.Printf("%.1f\n", value*(*factor)) | ||
| } else { | ||
| fmt.Printf("%.1f\n", value) | ||
| } | ||
| } | ||
|
|
||
| // Run runs with an optional verbose flag. | ||
| func Run(verbose *bool) { | ||
| if verbose != nil && *verbose { | ||
| fmt.Println("running verbose") | ||
| } else { | ||
| fmt.Println("running quiet") | ||
| } | ||
| } | ||
|
|
||
| // Delay delays with an optional extra duration. | ||
| func Delay(base time.Duration, extra *time.Duration) { | ||
| if extra != nil { | ||
| fmt.Printf("delay %s + %s\n", base, *extra) | ||
| } else { | ||
| fmt.Printf("delay %s\n", base) | ||
| } | ||
| } | ||
|
|
||
| // AllOptional takes only optional args. | ||
| func AllOptional(a *string, b *int) { | ||
| if a != nil { | ||
| fmt.Printf("a=%s\n", *a) | ||
| } else { | ||
| fmt.Println("a=<nil>") | ||
| } | ||
| if b != nil { | ||
| fmt.Printf("b=%d\n", *b) | ||
| } else { | ||
| fmt.Println("b=<nil>") | ||
| } | ||
| } | ||
|
|
||
| // Say says the message with optional capitalization and repeat count. | ||
| func Say(msg string, cap *bool, count *int) { | ||
| if cap != nil && *cap { | ||
| msg = strings.ToUpper(msg) | ||
| } | ||
| repeat := 1 | ||
| if count != nil { | ||
| repeat = *count | ||
| } | ||
| for i := 0; i < repeat; i++ { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
|
|
||
| // Announce prints an announcement. | ||
| func Announce(msg string) { | ||
| fmt.Printf("Announcement: %s\n", msg) | ||
| } | ||
|
|
||
| // Mixed tests interleaved required and optional args with context. | ||
| func Mixed(ctx context.Context, name string, greeting *string, count int) error { | ||
| g := "Hello" | ||
| if greeting != nil { | ||
| g = *greeting | ||
| } | ||
| for i := 0; i < count; i++ { | ||
| fmt.Printf("%s, %s!\n", g, name) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.