This is newbie question... Let's say I have a Go code in a directory(repository initiated here as root) called "myprogram". And I write some packages divided in several sub-directories.
Then I have
repository, root directory
myprogram
----------- package1
----------- package2
----------- package3
Then in the myprogram directory, I will write the code with main package for main program and the main program will call all the packages that are defined in the sub-directories, like the following:
main.go
import "github.com/username/package1"
import "github.com/username/package2"
import "github.com/username/package3"
func main() {
package1.Function1()
....
}
Then I can just run this code with
$ go run main.go
I have no problem so far. But what if this program has some features with flags?
$ go run main.go flag1 flag2
This works, but I want to run something like
$ myprogram
$ flag1
....
$ flag2
Since all the programs and source code will be contained under the directory myprogram which is also the name of the project, repository
myprogram
----------- package1
----------- package2
----------- package3
We run Vim with the command, something like this...
$ vim
To summarize, how do I make my program run by command, not by go run main ?
Is there any open source repository that I can refer to? Or please let me know the package or documentation to read. I tried
go install
But can't make it work like I wanted.
Thanks a lot!
Use go build
. This will create an executable of the package in the current directory. You can also run go build <packagename>
, which creates a binary in $GOPATH/bin
.