...

/

Custom Packages and Visibility

Custom Packages and Visibility

This lesson provides a detailed description on how to create and successfully run a custom package in any Go IDE.

Packages are the primary means in Go for organizing and compiling code. A lot of basic information about packages has already been given in Chapter 2, most notably the Visibility rule. Now we will see concrete examples of the use of packages that you write yourself. By custom packages, we mean self-written packages or packages otherwise external to the standard library.

When writing your packages, use short, single-word, lowercase names without _ for the filename(s).

The go command

The go command is used as a uniform (OS independent) tool to fetch, build, and install Go packages and applications. Everything from how to get a package, how to compile, link and test it, is deduced by looking at the source code to find dependencies and determine build conditions. A single environment variable: GOPATH (which is discussed in Appendix) tells the go command (and other related tools) where to find and install the Go packages on your system.

This command has the same syntax as the system’s PATH environment variable, so a GOPATH is a list of absolute paths, separated by colons (:) on Unix-like machines and by semicolons (;) on Windows machines.

On Unix a typical value could be:

GOPATH=/home/user/ext:/home/user/go_projects

On Windows a typical value could be:

GOPATH=d:\go_projects;e:\go_projects

Each path in the list (in this case e:\go_projects or/home/user/go_projects) specifies the location of a workspace.

A workspace contains Go source files and their associated package objects, and command executables. It has a prescribed structure of three subdirectories:

• src contains a structure of subdirectories each containing Go source files (.go)

• pkg contains installed and compiled package objects (.a)

• bin contains executables (.out or ...