Packages
Example
https://yingshaoxo.blogspot.com/2021/06/how-to-create-package-with-golang.html
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:
If you run main.go
οΌyou'll get:
That tells us some facts:
golang
package was separated byfolder
. In that folder, all your.go file
must declare thepackage name
bypackage *
you can
import
your own packages frommain.go
with./your_package_name
before golang import any package, it will run the
init()
function firstif 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.
Last updated