godtail
godtail

Reputation: 189

Why Go put all code in $GOPATH/src

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.

Upvotes: 3

Views: 7159

Answers (2)

ospider
ospider

Reputation: 10401

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.

Upvotes: 0

user6169399
user6169399

Reputation:

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.

Upvotes: 2

Related Questions