golang tutorial - Golang Command Line Arguments | Go Command | Command Line Arguments In Golang - golang - go programming language - google go - go language - go program - google language



Golang Command Line Arguments

  • A command-line argument or parameter is an item of information provided to a program when it is started.
  • A program can have many command-line arguments that identify sources or destinations of information, or that alter the operation of the program.
  • When a command processor is active a program is typically invoked by typing its name followed by command-line arguments (if any). For example, in Unix and Unix-like environments, an example of a command-line argument is:
    • rm file.s
  • "file.s" is a command-line argument which tells the program rm to remove the file "file.s".
  • Some programming languages, such as C, C++ and Java, allow a program to interpret the command-line arguments by handling them as string parameters in the main function. Other languages, such as Python, expose these arguments as global variables.
package main

	import "os"
import "fmt"

	func main() {
// os.Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, and os.Args[1:] holds the arguments to the program.
	    argsWithProg := os.Args
    argsWithoutProg := os.Args[1:]
// You can get individual args with normal indexing.
	    arg := os.Args[3]

	    fmt.Println(argsWithProg)
    fmt.Println(argsWithoutProg)
    fmt.Println(arg)
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output for the above go program

// 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

Related Searches to Golang Command Line Arguments | Go Command | Command Line Arguments In Golang