On Wed, Apr 11, 2018 at 6:11 AM, Robert P. J. Day <[email protected]> wrote: > > total beginner question here, but the docs seem vague or > inconsistent on what should be a simple question -- what does it mean > to import a name that is a single .a file versus importing a directory > name from under GOROOT (in my case, on fedora, /usr/lib/golang). > > first, running on fedora 28 beta, go version 1.10.1, and i see the > directory structure /usr/lib/golang and, under that, the further > directory layout pkg/linux_amd64, which i assume is where go will > begin its search for packages i specify to import -- that directory > contains, at its top level: > > $ ls -F > archive/ debug/ html/ math/ plugin.a sync.a > bufio.a encoding/ html.a math.a reflect.a syscall.a > bytes.a encoding.a image/ mime/ regexp/ testing/ > cmd/ errors.a image.a mime.a regexp.a testing.a > compress/ expvar.a index/ net/ runtime/ text/ > container/ flag.a internal/ net.a runtime.a time.a > context.a fmt.a io/ os/ sort.a unicode/ > crypto/ go/ io.a os.a strconv.a unicode.a > crypto.a hash/ log/ path/ strings.a vendor/ > database/ hash.a log.a path.a sync/ > $ > > so far, so good. > > now, when i, for example: > > import "fmt" > > i assume that what is being imported is the package represented by > that single archive file, fmt.a, correct? that seems simple enough, as > there is no fmt/ directory, so my initial understanding is that an > import is meant to import the contents of a single ".a" go package > file. > > consider, next, an example where there is both an archive file and a > corresponding subdirectory, say "hash.a" and "hash/". if i simply did > this: > > import "hash" > > i'm *assuming* that would import the package corresponding to only the > archive file "hash.a", correct? if i wanted to import a hash > "subpackage" (say, hash/crc32.a), then the import would look like: > > import "hash/crc32" > > which would import *only* that hash-related package. (i realize this > all sounds trivial, it would just be nice if the docs came right out > and said it.) > > finally, what if there is no top-level .a file, and *only* a > subdirectory, such as for container/: > > $ tree container > container > ├── heap.a > ├── list.a > └── ring.a > $ > > given that there is no top-level container.a file, would it even > make any sense to say: > > import "container" > > i realize this is all trivially trivial, but it would be nice if, > early in the docs, this was spelled out clearly and directly (unless, > of course, it is and i just haven't got to that section yet).
The .a files under $GOROOT/pkg are most usefully thought of a cache of compiled code. When you import "fmt", you are importing the package named "fmt" in $GOROOT/src. For any general non-relative import "a/b/c" the go tool will look in $GOROOT/src then in the src directory of each entry in $GOPATH. The package in one of those src directories is always what is being imported. The go tool may then decide to save some time by using the cached precompiled .a file in $GOROOT/pkg or $GOPATH/pkg. In future releases of Go, it is quite possible that $GOPATH/pkg, and maybe even $GOROOT/pkg, will go away entirely, to be replaced by $GOCACHE. So the documentation can and, I hope, does, ignore the .a files. They aren't relevant for understanding how the system works. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
