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.
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.
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.
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.
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.
Finally we can now use:
go run .
thanks to: https://github.com/golang/go/issues/22726#issuecomment-345841019
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With