Go to Go - Packages16 Mar 2014
Go is modern language, so it has to take care of current software development practices; code reuse or DRY (Don’t Repeat Yourself) being one of them. In go code can be packed into modules and shipped. Lets look into how go uses packages.
Importing a Packages
A go package can be imported using
import command, like we see in daily uses.
Creating a Package
To create a package, first we need to create a folder somewhere under our
GO_PATH/src directory with the same name as package, that we want to declare. Let us create a
math package under
Now open this file
math.go and create an Avarage method
Note method name starts from capital letter, in Go, only methods, whose name starting from capital letter are imported, not others.
Now to make it package, we need to install it. run
go install in
math directory. This will create a
math.a file under
$GO_PATH/pkg/github.com/go_to_go/pack_it; verify same.
$GO_PATH/src/github.com/go_to_go/pack_it and create a
pack.go to use this package