golang的哪些坑爷事: package实践
在golang中package是个困惑的概念, 特别是package还可以与folder不同名, 委实让我恶心了一把.
关于golang的package的最佳实践:
package is folder.
package name is folder name.
package path is folder path.
信耶稣得永生!
1. 同一个folder存在不同package, 编译错误:
D:/go/bin/go.exe build [E:/cgss/src/pkg01]
can't load package: package pkg01: found packages pkg01 (pkg01.go) and pkg012 (pkg02.go) in E:\cgss\src\pkg01
在同一个folder存在多个package, 则加载失败. 即使是main, 也一样
D:/go/bin/go.exe build [E:/cgss/src/test]
can't load package: package test: found packages main (cgss.go) and file01 (file01.go) in E:\cgss\src\test
2. folder name与package name不同(即package path与package name不同), 则需要使用
"import alias path"语法, 即:
import <package_name> <package_path>
否则编译错误: package name不是合法标识符.
D:/go/bin/go.exe build [E:/cgss/src/test]
can't load package: package test: found packages main (cgss.go) and file01 (file01.go) in E:\cgss\src\test
package main
import "fmt"import pkg012 "pkg01"
func main() { fmt.Println(pkg012.PKG_NAME)}