Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all .go files within current directory through the command line (multi file package)

I'm a newcomer to Go. I extremely like the language, but I quickly realised that I needed to start dividing my files due to an increase in program size.

go run main.go (with main.go having been the file with my main() function)

didn't work and I hit a barrier for a while, because I had no clue how to get my program working.

Some quick searching lead me to the answer of

go run main.go other.go ..

where by typing all the files that my package main consists of, I could get the programming running. However, this is utterly cumbersome and frustrating to do each time.

I write the following self-answered question in order to prevent others like myself who may again hit this barrier.

like image 982
dk123 Avatar asked May 16 '14 12:05

dk123


People also ask

How do I run .go files?

To run a go program, create a file with an extension .go and write your golang code in that file. For example, we will create a file named helloworld.go and write the following code in it. Now open command prompt and navigate to the location of helloworld.go file.

How do I run multiple files at once?

If you are trying to run multiple files on localhost using gorilla mux in go as per latest version(1.11). Try using any of the following 2 commands. go install && FolderName -port 8081 . go build && ./FolderName -port 8081.

Can a go package have multiple files?

A package can have many files but only one file with main function, since that file will be the entry point of the execution. If a package does not contain a file with main package declaration, then Go creates a package archive ( . a ) file inside pkg directory. Since, app is not an executable package, it created app.

What is go run command?

The go run command compiles and runs a main package comprised of the .go files specified on the command line. The command is compiled to a temporary folder. The go build and go install examine the files in the directory to determine which .go files are included in the main package.


1 Answers

Finally we can now use:

go run . 

thanks to: https://github.com/golang/go/issues/22726#issuecomment-345841019

like image 97
Macilias Avatar answered Sep 18 '22 20:09

Macilias