← All Articles

How to Read Program Arguments in Go

Posted on

There seems to be two schools of program options reading in programming languages. Those who have a main method where arguments are explicitly mentioned in the method signature, and those who have another way of reading the data. Go has a main method, but it has no arguments in its signature - you read the program arguments from os.Args (which requires you to import the os package).

package main

import "os"
import "fmt"

func main() {

    args := os.Args[1:]

    for _,element := range args {
      fmt.Println("Hello", element)