您的位置:首页 > 编程语言 > Go语言

组织Go的源码

2015-10-08 22:17 387 查看
工作空间(workspace)

在开始编写代码前,要先创建一个文件夹作为工作空间,工作空间文件夹里包含三个子文件夹

src 存放Go源码,以package的形式组织源码,每个package对应一个文件夹

pkg 类库编译后的二进制文件(*.a)存放在该文件夹里

bin 执行go install命令后,最终编译好的可执行文件会存放在该文件夹里

设置环境变量

变量GOPATH指向工作空间所在的目录,并把$GOPATH/bin加入PATH变量中

变量GOROOT指向Go的安装路径,并把$GOROOT/bin加入PATH变量中

在编译Go源码时,会先去$GOROOT/src查找所需的package,如果找不到则查找$GOPATH/src

​package

Go源码文件的第一行必须是声明package

package name

where name is the package's default name for imports. (All files in a package must use the same name.)

Go's convention is that the package name is the last element of the import path: the package imported as "crypto/rot13" should be named rot13.

可执行的程序必须使用package main

There is no requirement that package names be unique across all packages linked into a single binary, only that the import paths (their full file names) be unique.

官方详细说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: