Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing CLI arguments to excutables with 'go run'

Tags:

go

I have a program readfile.go and I want to give the command line argument os.Args[1] also as readfile.go.

However 'go run' thinks that it is an additional argument to itself rather than to the output executable. Is there a flag which can tell 'go run' that this is an argument to executable?

mvaidya@mvaidya-VirtualBox:~/junkeork$ go run readfile.go readfile.go
package main: case-insensitive file name collision: "readfile.go" and "readfile.go"
mvaidya@mvaidya-VirtualBox:~/junkeork$

Error:

package main: case-insensitive file name collision: "readfile.go" and "readfile.go"

like image 498
forvaidya Avatar asked Jul 15 '17 11:07

forvaidya


People also ask

How do I pass a command line argument to exe?

Open a command prompt (Windows+R, type "cmd" and hit enter). Then change to the directory housing your executable ("cd enter-your-directory-here"), and run the command with the parameters.

How do you pass command line arguments in Golang?

To access all command-line arguments in their raw format, we need to use Args variables imported from the os package . This type of variable is a string ( [] ) slice. Args is an argument that starts with the name of the program in the command-line. The first value in the Args slice is the name of our program, while os.

How do I pass a command line argument in CMD?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

What happens if we run the program without passing the command line arguments?

If we run this class without any arguments, the output will be as follows. Now, let's pass some arguments to the main class. We have to pass the arguments as space-separated values. Note: If you are using Java 11 or higher, you don't need to compile the java source file explicitly.


1 Answers

You can use -- to separate gofiles from arguments:

go run readfile.go -- readfile.go
like image 126
janos Avatar answered Sep 30 '22 04:09

janos