I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).
Example of directory structure:
whatever.io/ myproject/ main.go myproject.go
The go compiler needs the package main and func main to start execution. My library needs the package myproject where I put stuff on it. This is what the go tool says when I am building another project that tries to import myproject:
main.go:5:2: found packages myproject (myproject.go) and main (main.go) in $GOPATH/src/whatever.io/myproject
So I believe there is no way to do it.
Should I move the library or the CLI to another package?
Just move your packages inside a new folder within the same directory of main.go. Remember to import the new package from the reference of the $GOPATH.
Example:
user@user:~/p/go/test/so-multipack$ ls -R .: a main.go ./a: a.go user@user:~/p/go/test/so-multipack$ cat main.go package main import ( "../so-multipack/a" ) func main(){ a.Hello() } user@user:~/p/go/test/so-multipack$ cat a/a.go package a import ( "fmt" ) func Hello(){ fmt.Println("hello from a") } user@user:~/p/go/test/so-multipack$ go run main.go hello from a user@user:~/p/go/test/so-multipack$ go build user@user:~/p/go/test/so-multipack$ ls a main.go so-multipack user@user:~/p/go/test/so-multipack$
Useful link:
go build vs go build file.go
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