8. Custom Package

Let's write a package to import and use in hellogo.

1

Assignment β€” create a library package

Follow these steps from the parent directory of hellogo (if your shell from the last lesson is still open then you're already there).

  • Create a sibling directory mystrings and enter it.

  • Initialize a Go module for the package.

  • Create mystrings.go with a Reverse function.

  • Build the package to check for compile errors.

You can run and submit the CLI tests from the root of the mystrings package.

2

Step: Navigate to the parent directory

If you're still in hellogo:

shell
cd ..

You should now be in your workspace directory.

3

Step: Create mystrings directory

shell
mkdir mystrings
cd mystrings
4

Step: Initialize the module

Run:

shell
go mod init {REMOTE}/{USERNAME}/mystrings

(Replace {REMOTE}/{USERNAME} with your actual module path, e.g. github.com/yourusername/mystrings.)

5

Step: Create mystrings.go

Create the file and paste the following:

mystrings.go
// by convention, we name our package the same as the directory
package mystrings

// Reverse reverses a string left to right
// Notice that we need to capitalize the first letter of the function
// If we don't then we won't be able to access this function outside of the
// mystrings package
func Reverse(s string) string {
  result := ""
  for _, v := range s {
    result = string(v) + result
  }
  return result
}

Only capitalized names are exported (accessible by other packages). Uncapitalized names are private.

6

Step: Note β€” no main

There is no main.go or func main() in this package. It's a library package (package mystrings), not package main.

7

Step: Build to check for errors

From the mystrings directory run:

shell
go build

Because this isn't a main package, go build won't create an executable. It will compile the package and save it to the build cache β€” useful for detecting compile errors. Successful builds produce no output.

8

Step: Run tests

From the root of the mystrings package run the provided test harness:

shell
bootdev test

Your Directory Structure Now

~/workspace/
β”œβ”€β”€ hellogo/
β”‚   β”œβ”€β”€ go.mod
β”‚   └── main.go
└── mystrings/
    β”œβ”€β”€ go.mod
    └── mystrings.go

Understanding the Code

Package declaration

package mystrings
  • Package name matches the directory (convention).

  • Not package main, so it's a library.

Exported vs Unexported

Exported (capitalized) functions are accessible from other packages:

Unexported (lowercase) functions are private to the package:

How Reverse works

Example: Input "hello" β†’ Output "olleh" (each rune is prepended to build the reversed string).

Testing export rules (example)

You can split exported and unexported logic like this:

Other packages can call Reverse() but not reverse().

Key Takeaways

  • Library packages don't have package main or func main().

  • Capitalized identifiers are exported (public).

  • Lowercase identifiers are unexported (private).

  • Package name should match the directory name.

  • go build on a library checks for errors but doesn't create an executable.