Go command-line arguments

last modified April 11, 2024

In this article we show how to pass command-line arguments into Go programs.

Command-line arguments

Command-line arguments are options and data that are passed to programs. We usually pass arguments to console programs, but sometimes we pass arguments to GUI programs as well.

The os.Args holds the command-line arguments. The first value in this slice is the name of the program, while the os.Args[1:] holds the arguments to the program. The individual arguments are accessed with indexing operation.

$ go version
go version go1.22.2 linux/amd64

We use Go version 1.22.2.

package main

import (

func main() {

    prg_name := os.Args[0]
    fmt.Printf("The program name is %s\n", prg_name)

    names := os.Args[1:]

    for _, name := range names {

        fmt.Printf("Hello, %s!\n", name)

The example receives command-line arguments.

prg_name := os.Args[0]
fmt.Printf("The program name is %s\n", prg_name)

We get and print the first argument, which is the program name.

names := os.Args[1:]

We get all the received arguments.


We print the type which holds the arguments (slice).

for _, name := range names {

    fmt.Printf("Hello, %s!\n", name)

We go through the arguments and build a message from each of them.

$ go build read_args.go 
$ ./read_args Jan Peter Lucia
The program name is ./read_args
Hello, Jan!
Hello, Peter!
Hello, Lucia!

We build the program and run it. We pass the program three names on the command line.


Go os package - reference

In this article we have covered passing command-line arguments to Go programs.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Go tutorials.