golang項目結構與其餘語言相似,可是仍然有一些須要注意的地方。golang
go 命令依賴一個重要的環境變量:$GOPATH
,它表示GO項目的路徑,以下設置bash
export GOPATH=/home/t/gospace
對於GOPATH來講,容許多個項目目錄(Unix中爲「:」,Windows中爲「;」)。函數
在項目目錄中,通常包含三個文件夾,分別爲src
,pkg
和 bin
。各個文件夾功能以下,this
在golang中,模塊導入包括兩種導入方式:相對路徑和絕對路徑。spa
當前文件同一目錄的model目錄,可是不建議這種方式來importcode
import ( "./test1" "../test2" )
前提條件須要把該項目加入到golang的GOPATH中,源碼
import ( "project/module1" "project/module2/t" )
點操做的含義就是這個包導入以後在你調用這個包的函數時,你能夠省略前綴的包名,it
import . "fmt" func test() { Println("test") }
別名操做就是把包命名成另外一個名字編譯
import f "fmt" func test() { f.Println("test") }
操做實際上是引入該包,而不直接使用包裏面的函數,而是調用了該包裏面的init函數class
文件1: module/module1.go
package module1 import "fmt" func init() { fmt.Println("this is module1") }
文件2: main.go
package main import ( "fmt" _ "module" ) func main() { fmt.Println("this is a test") }
output:
this is module1 this is a test