ProgramGuru

Command-Line Arguments

Start with Command-Line Arguments

Command-line arguments are strings supplied after the executable name when a program starts.

What you’ll learn

  • Explain the main purpose of command-line arguments.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

os.Args holds the program name at index zero and any additional arguments after it. The flag package provides a standard way to declare and parse options, including default values and help output.

For an online compiler example with fixed output, this lesson demonstrates a parser built from a known string slice. Replace the sample slice with os.Args[1:] when you want to process actual arguments.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for argsParse strings into intentional command-line options.CLI argumentsStep 1Parse flagsStep 2Read typed valuesStep 3Run behaviorStep 4Parse strings into intentional command-line options.
Parse strings into intentional command-line options.

Tools, syntax and practical notes

For a real application use flag.Parse() with the default command-line flags and inspect os.Args if positional arguments are needed.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"flag"
	"fmt"
)

func main() {
	flags := flag.NewFlagSet("demo", flag.ContinueOnError)
	name := flags.String("name", "guest", "person to greet")
	if err := flags.Parse([]string{"-name", "Lia"}); err != nil {
		panic(err)
	}
	fmt.Println("Hello,", *name)
}

Output

Hello, Lia

Trace what the program does

A new FlagSet parses a fixed -name argument and assigns Lia to the string pointer. The program prints Hello, Lia and does not depend on runtime CLI input.

Avoid common mistakes

  • os.Args[0] is normally the invoked executable path, not your first user argument.
  • Flag parsing can fail; check its error when using a FlagSet.

Try it yourself

Add a boolean -verbose flag to this FlagSet and test both true and false inputs.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Where are user-supplied CLI arguments stored in a typical Go executable?
Choose one answer. You can try again.

Answer and explanation

os.Args[1:]

Correct. A new FlagSet parses a fixed -name argument and assigns Lia to the string pointer.

What you should remember

You have practiced Command-Line Arguments in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Environment Variables →