I'm trying to compile this example (which is saved as main.go
in current working dir):
package main
import (
"flag"
"log"
"runtime"
"github.com/nats-io/go-nats"
)
// rest of the code
This works:
$ go build ./
But this does not:
$ go tool compile -o main.o main.go
main.go:8:2: can't find import: "github.com/nats-io/go-nats"
Both of examples above were run on the same terminal with same environment variables, so I don't understand why the 2nd one does not work. I've tried -D
and -I
parameters with various directories as $GOPATH
, $GOPATH/src
and so on with no success.
I know what it's considered best practice to not use go tool compile
and so on, but my goal is to add my go source to existing C++ makefile project and useage of go tool
will make it more consistent.
Correct syntax of go tool compile
is as follows:
go tool compile -I $GOPATH/pkg/linux_amd64 -o main.o main.go
The problem was that by default, compile
performs lookup only in $GOROOT
and ignores $GOPATH
.
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