I want to distribute packages in binary form without including the source code.
my demo project directory structure is like this:
demo
├── greet
│ ├── greet.go
│ └── hi
│ └── hi.go
├── hello
│ └── hello.go
└── main.go
main.go:
package main
import (
"fmt"
"demo/greet"
"demo/hello"
)
func main(){
fmt.Println("greet:")
greet.Greet()
fmt.Println("hello:")
hello.Hello()
}
greet.go
package greet
import (
"demo/greet/hi"
"fmt"
)
func Greet(){
fmt.Println("Greet Call Hi")
hi.Hi()
}
hi.go
package hi
import "fmt"
func Hi(){
fmt.Println("Hi")
}
hello.go
package hello
import (
"fmt"
"demo/greet"
)
func Hello(){
fmt.Println("hello call greet")
greet.Greet()
}
And I do this:
[user@localhost greet]$ go install -a ./...
It generated greet.a and greet/hi.a in $GOPATH/pkg/linux_amd64/demo. Then I edit hi.go and greet.go.
[user@localhost greet]$ cat greet.go
//go:binary-only-package
package greet
[user@localhost greet]$ cat hi/hi.go
//go:binary-only-package
package hi
Then I run main.go, I get errors:
[user@localhost greet]$ cat hi/hi.go
# command-line-arguments
cannot find package demo/greet/hi (using -importcfg)
/home/user/go/pkg/tool/linux_amd64/link: cannot open file : open : no such file or directory
greet is the package I want to distribute. If I delete package hi, main.go can run well.
demo
├── greet
│ └── greet.go
├── hello
│ └── hello.go
└── main.go
install:
[user@localhost greet]$ go install .
[user@localhost greet]$ vim greet.go
//go:binary-only-package
package greet
[user@localhost greet]$ cd ..
[user@localhost demo]$ go run main.go
greet:
Greet ...
hello:
hello call greet
Greet ...
[user@localhost demo]$
So my main problem is: how can I build binary lib and others can't see my source code. And the package has many sub-package.(If the package haven't sub-package, I use //go:binary-only-package
method can work well)
Please help or try to give some ideas how to solve this. Thanks in advance.