简体   繁体   中英

Why “go build” cannot find package?

I installed a test0 package to $gopath\\pkg\\windows_386\\hello\\test0.a , but when i build a main package which depends on the test0 package, the compiler says: import "hello/test0": cannot find package .

why this happens?

I have two go file:

$gopath/src/hello.go

package main

import (
    "fmt"
    "hello/test0"
)

func main() {
    fmt.Println(test0.Number)
}

$gopath/src/hello/test0/test0.go

package test0

const (
    Number int = 255
)

At first, i run go install hello/test0 , and it generated $gopath\\pkg\\windows_386\\hello\\test0.a

then, i delete the directory $gopath/src/hello

finally, i run go build hello.go , and the compiler sayed hello.go:5:2: import "hello/test0": cannot find package

That doesn't seem to be normally possible for the moment : https://code.google.com/p/go/issues/detail?id=2775

Maybe for Go1.1

A trick (that I didn't test) by Dave :

For a package called "hello", the go tool will look for .go sources in $GOPATH/src/hello, and only rebuild if the timestamp of the .a file is before the latest timestamp of the .go files. An easy way to fool it into accepting just the .a file is to drop a dummy .go file in the correct src directory and set its timestamp to before that of the .a file.

(this is a community answer, using what is said on golang-nuts ).

Why did you delete the sources? The build command in the go tool is for building a package and all it's dependencies. To do this it checks the sources of the packages to see if they need to be built due to changes. If it can't find them it will treat them as if they are not installed.

If you really want to deal with just the binary distributions you will need to use the compiler and linkers directly. You can find documentation on those here: http://golang.org/cmd/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM