GoGo
- src 包含项目的源代码文件;
- pkg 包含编译后生成的包/库文件;
- bin 包含编译后生成的可执行文件。
可以通过下面的例子来说明工程目录的组织管理。(Windows 7 64位,go version go1.3.3 windows/amd64)
1. 创建一个库文件
a.go
package myfunc
import "fmt"
func Afunc(str string) {
fmt.Println("a.go is package mufunc.")
fmt.Println(str)
}
这时候目录结构如下:
<dirtest>
|--<src>
|--<mufunc>
|--a.go
2. 创建main
scr/main.go
package main
import "myfunc"
func main() {
myfunc.Afunc("b.go is package main.")
}
这时候目录结构如下:
<dirtest>
|--<src>
|--<mufunc>
|--a.go
|--<main>
|--b.go
3. 使用go build
go build
E:\dirtest>go build src\main\b.go
src\main\b.go:3:8: cannot find package "myfunc" in any of:
C:\Program Files\go\src\pkg\myfunc (from $GOROOT)
D:\GoLang\src\myfunc (from $GOPATH)
Go$GOROOTmyfunc$GOPATHgo envGo
E:\dirtest>go env
set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\GoLang
set GORACE=
set GOROOT=C:\Program Files\go
set GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
E:\dirtest$GOPATH
go buildb.exe
E:\dirtest\src\main>go env
set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\GoLang;E:\dirtest
set GORACE=
set GOROOT=C:\Program Files\go
set GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
E:\dirtest>go build src\main\b.go
E:\dirtest>dir
E:\dirtest 的目录
2015/01/13 23:11 <DIR> .
2015/01/13 23:11 <DIR> ..
2015/01/13 23:11 1,958,912 b.exe
2015/01/13 22:52 <DIR> src
E:\dirtest>b.exe
a.go is package mufunc.
b.go is package main.
go installgo install.go
go installE:\dirtestbin\main.exeD:\GoLang
go install
E:\dirtest\src\main>go install
go install: no install location for E:\dirtest\src\main: hidden by D:\GoLang\src
\main
$GOPATH$GOPATH
go install myfuncmyfunc.ago install mainmain.exe
<dirtest>
|--<src>
|--<mufunc>
|--a.go
|--<main>
|--b.go
|--<pkg>
|--<windows_amd64>
|--myfunc.a
|--<bin>
|--main.exe
现在就算是成功完成了一个示例“项目”吧...
4. 常见错误除了上面的步骤中出现的错误,其实工程目录管理稍有不慎,就会出现其他问题,例如:
a.gob.gomyfunc
这时候的目录结构如下:
<dirtest>
|--<src>
|--<mufunc>
|--a.go
|--b.go
go installgo buildgo run
E:\dirtest\src\myfunc>go install
can't load package: package myfunc: found packages myfunc (a.go) and main (b.go)
in E:\dirtest\src\myfunc
E:\dirtest\src\myfunc>go build
can't load package: package myfunc: found packages myfunc (a.go) and main (b.go)
in E:\dirtest\src\myfunc
E:\dirtest\src\myfunc>go run b.go
b.go:3:8: found packages myfunc (a.go) and main (b.go) in E:\dirtest\src\myfunc
从参考资料3中可以看到,每个子目录中只能存在一个package,否则编译时会报错,所以一个子目录下面不能包含多个不同包的源文件。
2. 一个项目能包含多个main()吗?
c.gomyfunc
package main
import "fmt"
import "myfunc"
func main() {
fmt.Println("This is single c.go")
myfunc.Afunc("c.go is also package main.")
}
执行相应的命令,结果如下:
E:\dirtest\src\main>go build
# main
.\c.go:4: main redeclared in this block
previous declaration at .\b.go:5
E:\dirtest\src\main>go build c.go
# 成功,当前目录下生成了c.exe
E:\dirtest\src\main>go install
# main
.\c.go:4: main redeclared in this block
previous declaration at .\b.go:5
E:\dirtest\src\main>go install c.go
go install: no install location for .go files listed on command line (GOBIN not
set)
E:\dirtest\src\main>go run c.go
This is single c.go
a.go is package mufunc.
c.go is also package main.
go rungo build c.goc.go
E:\dirtest\src\cmain>dir
E:\dirtest\src\cmain 的目录
2015/01/14 11:27 <DIR> .
2015/01/14 11:27 <DIR> ..
2015/01/14 11:24 147 c.go
E:\dirtest\src\cmain>go build
E:\dirtest\src\cmain>go install
go installexe
3. go install: no install location for .go files listed on command line (GOBIN not set)
go install$GOROOT$GOPATH$GOROOT/bin$GOPATH/bin$GOBIN=E:\dirtest\bin
E:\dirtest\src\cmain>go env
set GOARCH=amd64
set GOBIN=E:\dirtest\bin
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\dirtest;D:\GoLang
set GORACE=
set GOROOT=C:\Program Files\go
set GOTOOLDIR=C:\Program Files\go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
E:\dirtest\src\cmain>go install c.go
# 成功在 E:\dirtest\bin 下面生成 c.exe
go install
4. go build、go install 和 go run 的区别
详细的可以查看参考资料4,这里简单说一下:
go buildmain.ago install$GOPATH/bin$GOPATH/pkggo run gofiles...maingo run: cannot run non-main package
go run$GOPATHgo buildgo installgo run
5. 总结
$GOPATHsrc/proj1src/proj2go install