Search⌘ K

Custom Packages: Folder Structure, go install and go test

Explore how to build and organize custom Go packages with best practice folder structures. Learn to install packages using go install and verify code quality using go test. Understand workspace setup, testing conventions, and OS-specific code handling for Go applications.

For demonstration, we take a simple package uc which has a function UpperCase to turn a string into uppercase letters. This is just for demonstration purposes (it wraps the same functionality from package “strings”), but the same techniques can be applied to more complex packages.

Folder-structure for custom packages

The following structure is considered best practice and imposed by the go tool (where uc stands for a general package name; the names in bold are folders; italicized is the executable):

go_projects (a workspace in $GOPATH)
           src/uppercase

                        /uc (contains go code for package uc)
                            uc.go
                            uc_test.go
                        /uc_main
                            ucmain.go (main program for using package uc)
           
           pkg/windows_amd64 (the actual name depends on your operating system/architecture)

                        /uppercase
                        uc.a (object file of package)

bin                     (contains the final executable files)

                         uc_main.exe

Building the package uc in the uc folder

The ...