Go Package Structure

All Go source code must belong to a package. Declare a package in go using the following syntax…

package <name>

Package main

Go programs start in the main package.

Example

package main

This serves as the entry point for an executable.

Importing Packages

You can import either by mutliple import or factored import

Example

// Multiple import 
import "fmt"
import "net/http"
import "time"
// Factored import 
import (
    "fmt",
    "net/http",
    "time",
)

Exported vs Unexported Symbols and Naming Convention

Exported symbols allows the symbol to be used in other packages

Exported symbols is spelt via PascalCasing Go convention for symbols is to use camelCasing (unexported)

Circular Dependencies

Go does not allow circular dependencies

Creating Custom Packages

Packages allow programs to split up source code to seperate files instead of keeping it all in package main

To create a Go package

go mod init <top-package-name>

Example

go mod init indigo

which should result in a go.mod file in current working directory

Structuring the Project

Rules for package structure

There are 3 rules for structuring the project

Custom Packages must be in a seperate directory

e.g.

Package Name must be the same as Directory

package <name> name must be the same name as the directory the file is currently in

Example

TODO!