Command-Line Argument Handling in Go (Challenge)

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The purpose of this challenge is to practice working with command-line arguments in Golang.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/CommandLineandEnvironmentGroup(["`Command Line and Environment`"]) go/CommandLineandEnvironmentGroup -.-> go/command_line("`Command Line`") subgraph Lab Skills go/command_line -.-> lab-15382{{"`Command-Line Argument Handling in Go (Challenge)`"}} end

Command-line arguments

The program currently prints out the raw command-line arguments passed to it. However, it needs to be modified to print out specific arguments based on their index.

Requirements

  • Basic knowledge of Golang
  • Familiarity with command-line arguments

Example

## To experiment with command-line arguments it's best to
## build a binary with `go build` first.
$ go build command-line-arguments.go
$ ./command-line-arguments a b c d
[./command-line-arguments a b c d]
[a b c d]
c

## Next we'll look at more advanced command-line processing
## with flags.

Summary

In this challenge, we learned how to access and print out specific command-line arguments in Golang. By using the os.Args variable and indexing into it, we can easily access the arguments passed to the program.

Other Go Tutorials you may like