How can I find my package?

Where should I put my package so that it can be imported by another package?

$ tree . ├── main.go └── src └── test.go 1 directory, 2 files $ cat src/test.go package test $ cat main.go package main import "test" $ go build main.go main.go:3:8: import "test": cannot find package 
+11
go
May 15 '12 at 12:35
source share
3 answers

Install GOPATH. Put your foo source (s) package in GOPATH / src / optional-whatever / foo / *. And use it in code like

 import "optional-whatever/foo" 

You do not need to explicitly install foo, the go tool is a build tool, it will do this automatically if necessary.

+8
May 15 '12 at 13:06
source share

There are a few things that must happen. First you need to install the "test" package:

 $ export GOPATH=$(pwd) # Assumes a bourne shell (not csh) $ mkdir src/test $ mv src/test.go src/test/test.go $ mkdir pkg # go install will put packages here $ go install test # build the package and put it in $GOPATH/pkg $ go build main.go 

Note that there is no need to create pkg, as go install will do it for you. After you installed the test package (usually a bad name, BTW) go build main.go , different errors should now appear (for example, "imported and not used")

+8
May 15 '12 at 12:45
source share

maybe you can put the test.go file in the same directory as main.go, and in test.go, it uses something like this:

 import "./test" 
-four
Jul 16 2018-12-12T00:
source share



All Articles