Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import path difference using "build" and "tool compile"

Tags:

go

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.

like image 326
Denis Sheremet Avatar asked Nov 28 '17 10:11

Denis Sheremet


1 Answers

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.

like image 104
Denis Sheremet Avatar answered Nov 01 '22 18:11

Denis Sheremet