How do you properly set up a Golang library?


I’ve tried many times to set up a REAL go package with the modules system and store code in pkg. All the tutorials I’ve found are too basic, creating a module with go files stores at the top level, and I keep getting no Go files in /usr/local/go/

I’ve tried a bunch of different things, but I can’t get it to work properly…

GOROOT is set to /usr/local/go. I created a package here /usr/local/go/



go 1.18


package mypackage

// Add is our function that sums two integers
func Add(x, y int) (res int) {
    return x + y

// Subtract subtracts two integers
func Subtract(x, y int) (res int) {
    return x - y


package mypackage

import "testing"

func TestAdd(t *testing.T){

    got := Add(4, 6)
    want := 10

    if got != want {
        t.Errorf("got %q, wanted %q", got, want)

And I run: go test

What am I doing wrong? I find Go so frustrating to set up because languages/runtimes like Rust and NodeJS have very friendly package managers and are real easy to setup.

I’m trying to structure a library as described in this guidance for structuring go packages.


Don’t confuse modules with packages.
One module might hold many packages.
Like this:

  • module_dir/package1_dir
  • module_dir/package2_dir

Try this layout:



In mypkg.go and mypkg_test.go declare package mypkg.

Otherwise, run this script and it will create a correct layout for you:

Answered By – Everton

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.