Libraries
Until now, most of our examples were applications. An application is defined by its main
function and package. But with Go, you can also create pure libraries. In libraries, the package need not be called main nor do you need the main
function.
As libraries aren't applications, you cannot build a binary file with them and you need the main
package that is going to use them.
For example, let's create an arithmetic library to perform common operations on integers: sums, subtractions, multiplications, and divisions. We'll not get into many details about the implementation to focus on the particularities of Go's libraries:
package arithmetic func Sum(args ...int) (res int) { for _, v := range args { res += v } return }
First, we need a name for our library; we set this name by giving a name to the entire package. This means that every file in this folder must have this package name too and the entire group of files composes...