Learning Golang: Packages

Share on:

This is part 3 of my journey learning Golang.

Package declaration

The first line of a Go source file is the “package declaration”, defined by the package keyword.

This serves a few purposes:

  • It provides a structure for grouping related source files.
  • It provides a mechanism for code reuse.
  • It differentiates between executable packages from utility packages (i.e. libraries).

Example:

1package main

go build will produce an executable binary file for source files with package main.

Importing a package

The import keyword allows bringing in and using code from other packages.

Example:

1import "fmt"

A “package reference variable” is created from the imported package’s name – in this case, fmt.

Importing multiple packages

It’s usual for a source file to import multiple packages. The convention for that is to use a single import statement with a package list within parenthesis like this:

1import (
2  "package1"
3  "package2"
4)

Package alias

It is possible to define an “alias” as a shorthand name instead of the default package reference variable. The syntax for that is to specify the alias before the package name like this:

1import (
2  p1 "package1"
3  "package2"
4)

That allows calling functions in package1 like this:

1p1.SampleFunc()

Full example

This is a full “Hello World” example in Go that demonstrates the concepts above.

 1package main
 2
 3import (
 4  "fmt"
 5  t "time"
 6)
 7
 8func main() {
 9  fmt.Println("Hello, World!")
10  fmt.Println("The time is now", t.Now())
11}

Highlights:

  • Line 1: The package declaration with a package name of main. That will make this an executable program.
  • Line 3: The import statement is importing a list of packages.
  • Line 4: Imports the “fmt” package with its standard name (“fmt”).
  • Line 5: Imports the “time” package with the alias “t”.
  • Line 9: Uses the Println function from package fmt to display “Hello, World!”
  • Line 10: Uses the Println function from package fmt to display “The time is now " followed by the current time, as returned by function Now from package time (aliased as t).

This is a sample output of the program above:

1$ go run main.go
2Hello, World!
3The time is now 2020-08-16 18:07:24.180778888 +0000 UTC m=+0.000097040

Takeaways

  • The package keyword is used for the obligatory package declaration.
  • The import keyword is used for bringing into context code from other packages.
  • main is a special package name for executable programs.

If you know Go, what would you add to an initial exposure on the concept of packages?


comments powered by Disqus