Like Python, sometimes, you want to separate your codes to different files.
So it can be organized.
In Python, you use import to load function from .py file.
In Golang, you will also use import to load function from .go file.
But Golang file structure is different.
For example,a tree of a project may look like this:
src
├── hi
│ └── a_go_file.go
└── main.go
//a_go_file.gopackagehiimport"fmt"funcinit(){fmt.Println("You are runing init() function from hi package")}funcSay_hello(){fmt.Println("You are runing Say_hello() function from hi package")}
If you run main.go,you'll get:
That tells us some facts:
golang package was separated by folder. In that folder, all your .go file must declare the package name by package *
you can import your own packages from main.go with ./your_package_name
before golang import any package, it will run the init() function first
if you want to export or use a function from a package, the first character of that function name must keep capitalizing.
Sometimes, people would like to put all their source codes to ~/go/src
In that case you will import a package using github.com/yourname/projectname
It's good to do with if you don't want to handle the folder-file structure with relative import
And by doing so, vim YouCompleteMe could be able to work correctly
By the way, in case you don't know: use `go get ./...` to install all missing packages for a Go project.