Software Engineering Essentials is a series of blog posts designed to help you get started with a wide variety of software engineering topics. This post was originally part of my Go-Twitter project, a project-based curriculum designed to take someone from “zero” to “competent” in the world of software engineering.
All writing for this post is licensed under CC-BY-4.0
, while all code is licensed under the MIT
license.
Command line arguments are one way to pass user input into your application.
Let's take a look at the common Unix utility ls
. ls
is a small application
that lists files and directories, just like double-clicking on a folder in
Windows Explorer, ls
allows you to peer into directories and get additional
information about files, like ownership information, size, last modification
timestamps, and more. ls
, like many command line applications, can take in
arguments:
ls some-directory
In this example ls
is the application you are running and some-directory
is
the argument. What you're asking ls
to do is give you a listing of all files
and folders inside of some-directory
.
Because programming is generally open-ended, you can utilize command line arguments to control any aspect of your program. You could use an argument to control which IP address and port your application listens for requests on, or what to print, or if an operation should target your test or production environments. Let's take a look at a simple example:
Here's a simple Go program that will print a person's name and age on the command line:
01-simple-arguments/main.go
:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf("My name is %s and my age is %s.", os.Args[1], os.Args[2])
}
So if we run go run example-code/02-command-line-arguments-and-flags/01-command-line-arguments/01-simple-arguments/main.go Bob 99
, we'll see: My name is Bob and my age is 99.
In this example, we use the os
package to get an array of arguments provided
on the command line. Arrays in Go start at 0
, so we're actually printing the
second and third argument. The first argument is the location of the program
we're running. Since we're using go run
, it will be in a temporary,
auto-generated location. Let's add this block of code to our program and look at
each argument individually:
fmt.Println("Our program's arguments:")
for i, arg := range os.Args {
fmt.Printf("os.Args[%d]: '%s'\\\\n", i, arg)
}
This code will print out each argument and its number:
Our program's arguments:
os.Args[0]: '/tmp/go-build201851007/b001/exe/main'
os.Args[1]: 'Bob'
os.Args[2]: '99'
So /tmp/go-build201851007/b001/exe/main
is the temporary compiled version of
our program, made by the go run
command. The other arguments, Bob
and 99
are the arguments we provided on the command line.
So, what happens if we provide less than 2 arguments to our program?
go run example-code/02-command-line-arguments-and-flags/01-command-line-arguments/01-simple-arguments/main.go Bob
panic: runtime error: index out of range [2] with length 2
goroutine 1 [running]:
main.main()
/home/samurailink3/git/go-twitter/example-code/02-command-line-arguments-and-flags/01-command-line-arguments/01-simple-arguments/main.go:9 +0x21c
exit status 2