Go language project setup in Windows

huangapple go评论163阅读模式
英文:

Go language project setup in Windows

问题

我是新手,想了解一下Go语言的环境设置。

有两件事我还不太明白,希望社区能在这里解答一下。

Go项目的设置是怎样的?go path是如何工作的?

我是从Windows的角度来问的。

  • Go项目有没有标准的设置方式?
  • 为什么所有的依赖都放在一个位置?
  • 是为了能在多个项目之间共享而不进行版本控制吗?
  • 我应该将go path设置为每个项目的文件夹吗?我不确定。
英文:

I am new to Go language and I wanted to understand how the Go environment is setup.

There is two things I dont quite understand yet and I was hoping the community could clarify here.

What is the Go project setup like and how does the go path work?

I am asking this from a windows point of view.

  • Do the Go projects have a standard way of being setup?
  • Why are all the dependencies going to one location?
  • Is it so they can be shared between multiple projects and not be version controlled?
  • Should I set the go path per project to the project folder? I dont know.

答案1

得分: 2

你所有问题的答案都在这里:http://golang.org/doc/code.html
你应该真正阅读那个页面,但总结起来,你需要按照这种方式组织你的Go工作空间的主要原因是,这样go工具(包括编译器和自动下载库的工具)可以完成它所做的一切。

当你安装一个go程序时,它会自动放在$GOPATH/bin中,当你构建一个库时,它会自动添加到$GOPATH/pkg中,这样你就可以拥有一个集中的位置来存放第三方库,从而避免不必要的库复制。你的项目应该位于$GOPATH/src/some_online_repository_location/project_name$GOPATH/src/project_name。路径的最后一部分是包名,但你应该在http://golang.org/doc/effective_go.html#names中阅读相关内容。

示例:对于你在github上托管的项目hello,你的go工作空间中的正确路径是:$GOPATH/src/github.com/youruser/hello/。这样,go工具就可以使用go get命令自动从在线仓库下载库。例如:go get github.com/youruser/hello

遵循这个“标准”可以让go工具在所有系统中轻松工作,无需任何配置,它还有一个副作用,即使所有go项目都以相同的方式结构化,因此当获取其他人的代码时,你知道它的结构方式,并且知道它将在你当前的go工作空间中工作。

阅读http://golang.org/doc/code.html文档,你应该会更好地理解它。如果有任何疑问,请留言。

英文:

The answer to all your questions are here: http://golang.org/doc/code.html
You should really read that page, but to sum up the main reason why you need to structure your go workspaces that way, is so the go tool (this includes the compiler and the tool for automatically downloading libraries) can do everything it does.

When you install a go program it is automatically put in $GOPATH/bin, and when you build a library it is automatically added to $GOPATH/pkg allowing you to have a centralized location for third party libraries and thus preventing you to do unnecessary copies of libraries. Your projects should be in $GOPATH/src/some_online_repository_location/project_name or $GOPATH/src/project_name. The last part of the path is the package name but you should read about that in http://golang.org/doc/effective_go.html#names

EXAMPLE: For you project hello that you host on github, the correct path on your go workspace is: $GOPATH/src/github.com/youruser/hello/. This allows the go tool to automatically download libraries from online repositories wit the command go get. Example: go get github.com/youruser/hello

Maintaining this "standard" allows the go tool to easily work in all systems without any configuration and it also has the side effect of making all go projects being structured in the same way, so when get someone else's code you know how it is structured and you know it will work on your current go workspace.

Read all the http://golang.org/doc/code.html document and you should understand it better.
Hope it helps, comment if you have any doubts.

答案2

得分: 1

一个例子

GOPATH = c:\gocode
应用程序有4个代码文件 = msg.go(主文件),web.go,channel.go,date.go
应用程序使用1个外部库 = martini

源代码位置
c:\gocode\src\app\msg\ (包名为msg)
... web.go,channel.go
c:\gocode\src\app\msg\main\ (包名为main)
... msg.go
c:\gocode\src\lib\ (包名为lib)
... date.go
c:\gocode\src\github.com\go-martini\martini (包名为martini)
... 所有martini库文件

文件使用的导入包(导入的是包而不是文件,所有路径都相对于$GOPATH/src)
msg.go
... 导入 "app/msg" > 提供对web.go、channel.go中导出元素的访问
... 导入 "lib" > 提供对date.go中导出元素的访问
web.go
... 导入 "lib" > 提供对date.go中导出元素的访问
... 导入 "github.com/go-martini/martini"

注意:
同一包中的所有文件都可以访问彼此的元素,即使是非导出的元素。
不需要或不允许导入,只需直接引用,就像它们在同一个文件中一样。

英文:
An Example

GOPATH = c:\gocode  
App has 4 code files = msg.go (main), web.go, channel.go, date.go  
App uses 1 external library = martini  

**Source Locations**:  
c:\gocode\src\app\msg\		(package is msg)  
... web.go, channel.go  
c:\gocode\src\app\msg\main\	(package is main)  
... msg.go  				
c:\gocode\src\lib\		(package is lib)  
... date.go  
c:\gocode\src\github.com\go-martini\martini	(pkg is martini)  
... all martini lib files    

**Imports Used by File** ( Import Pkgs Not Files, All Paths Relative to $GOPATH/src )  
msg.go  
... import “app/msg”  > provides access to exported elements in web.go, channel.go  
... import “lib” > provides access to exported elements in date.go  
web.go  
... import “lib” > provides access to exported elements in date.go  
... import “github.com/go-martini/martini”  

Note:  
All files in the same pkg can access each other’s elements, even non exported ones.  
No import needed or allowed, just a plain reference, as thought it was in the same file. 

huangapple
  • 本文由 发表于 2014年4月23日 00:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/23224700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定