3

When I make a package use go install, I found Go search the package form two path

  1. /usr/local/Cellar/go/1.6.2/libexec/src/ go install dir. many base package like fmt...

  2. /Users/godtail/Product/go/go_path/src/ $GOPATH/src. bad $GOPATH, I will change it.

also can use relative path, but is not recommended.

I always put my work dir like this.

Product A
    php
    nodejs
    go(i hope to put here)
Product B
    java
    nodejs
...

How can I do like this? or maybe change dir follow Go.
Give me some advice, thank you very much.

godtail
  • 189
  • 1
  • 10

2 Answers2

2

See: https://golang.org/ref/spec#Packages

Go programs are constructed by linking together packages. A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package. Those elements may be exported and used in another package.

Source file organization

Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.

SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .

You may add many src dirs to your $GOPATH:

Like the system PATH environment variable, Go path is a : delimited list of directories where Go will look for packages (;on Windows).

So you may add your project src dir to the $GOPATH.
Or just store your library packages in one $GOPATH.

Graham
  • 7,431
  • 18
  • 59
  • 84
  • Thank your for answer ~.i should move my library packages to the $GOPATH dir. also can improve reuse. and when i use git how can i push my lib package. or i have some product use the same lib package. – godtail Aug 17 '16 at 04:56
  • did go have package manger like composer or npm ? then i just need to edit the config json. and it can pull code from remote. – godtail Aug 17 '16 at 05:01
  • see: http://stackoverflow.com/questions/30300279/golang-dependency-management-best-practice –  Aug 17 '16 at 05:02
0

You don't need to do that. Golang is going to introduce an official tool called dep, which is like pip for python or npm for node.js.

ospider
  • 9,334
  • 3
  • 46
  • 46