dshkmamau65777662 2016-08-17 03:56
浏览 340
已采纳

为什么Go将所有代码都放在$ 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.

  • 写回答

2条回答 默认 最新

  • dongtang4019 2016-08-17 04:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题