Everything in Go is delivered in the form of packages. A Go package is a Go source file that begins with the package
keyword followed by the name of the package. Some packages have a structure. For example, the net
package has several subdirectories, named http
, mail
, rpc
, smtp
, textproto
, and url
, which should be imported as net/http
, net/mail
, net/rpc
, net/smtp
, net/textproto
, and net/url
, respectively.
Packages are mainly used for grouping related functions, variables, and constants so that you can transfer them easily and use them in your own Go programs. Note that apart from the main
package, Go packages are not autonomous programs and cannot be compiled into executable files. This means that they need to be called directly or indirectly from a main
package in order to be used. As a result, if you try to execute a Go package as if it was an autonomous program, you will be disappointed:
$ go run aPackage.go go run: cannot run non-main package