Created
February 10, 2021 20:57
-
-
Save Miqueas/a3920d03e2081477afb5216c4c3cd283 to your computer and use it in GitHub Desktop.
[Go] Basic example of using commandline arguments
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
package main | |
import ( | |
// For printing | |
Fmt "fmt" | |
// For arguments parsing | |
Flag "flag" | |
) | |
// Defines a flag for the commandline. Refers to the doc for details: | |
// https://golang.org/pkg/flag/ | |
var Name = Flag.String("name", "guest", "Name to greet") | |
var GreetB = Flag.Bool("greet-b", false, "Alternate the greet") | |
func main() { | |
// Initializes the argument parsing | |
Flag.Parse() | |
// Remember: values returned by the above functions are pointers, so | |
// you'll need to dereference it. | |
if !(*GreetB) { | |
Fmt.Printf("Hello, %s!\n", (*Name)) | |
} else { | |
Fmt.Printf("Hello, %s! How are you?\n", (*Name)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This results in something like this: