Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Created February 10, 2021 20:57
Show Gist options
  • Save Miqueas/a3920d03e2081477afb5216c4c3cd283 to your computer and use it in GitHub Desktop.
Save Miqueas/a3920d03e2081477afb5216c4c3cd283 to your computer and use it in GitHub Desktop.
[Go] Basic example of using commandline arguments
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))
}
}
@Miqueas
Copy link
Author

Miqueas commented Feb 10, 2021

This results in something like this:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment