Skip to content

Commit 305851c

Browse files
Merge pull request #49 from Aashna-Agrawal/command_line_use
Command Line Arguments Example
2 parents f998808 + b84dcb8 commit 305851c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

advanced/command_line_arguments.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
env := flag.String("env", "dev", "Environment(dev, qa, stg, prod)")
10+
cron := flag.Bool("consumer", false, "boolean")
11+
//Parse parses the command-line flags
12+
flag.Parse()
13+
fmt.Println("The environment set is", *env)
14+
fmt.Println("The consumer flag retrieved from command line is", *cron)
15+
}

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ Public-key/asymmetric cryptography signing and validating
535535
go run ppk-crypto.go
536536
```
537537

538+
Command Line Arguments Golang Example
539+
We can get argument values though command line by specifying the operator '-' with the name of the argument and the value to be set. E.g. -env=qa
540+
541+
```Shell
542+
go run command_line_arguments.go
543+
go run command_line_arguments.go -env=qa -consumer=true
544+
```
545+
538546
## Compile
539547

540548
One great aspect of Golang is, that you can start go applications via ```go run name.go```, but also compile it to an executable with ```go build name.go```. After that you can start the compiled version which starts much faster.

0 commit comments

Comments
 (0)