Skip to content

Commit a0c0aeb

Browse files
committed
get Command Line Arguments Example
1 parent edc1243 commit a0c0aeb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

advanced/command_line_arguments.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
flag.Parse()
12+
fmt.Println("The environment set is", *env)
13+
fmt.Println("The consumer flag retrieved from command line is", *cron)
14+
}

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)