Start a Go command-line project with a module and a test

Start a small Go command-line project with a module, a pure function and one test. Run the program and the test from the same module directory, then deliberately break an expectation once to check that the test is being discovered. This example uses the standard library and no external dependencies.

1. Make the project boundary explicit

Create an empty folder named hello and open a terminal inside it. Run go version first so that the toolchain in this terminal is visible. An editor and a terminal can use different installations; the result printed here identifies the command you are about to use. Follow the official installation instructions if the command is unavailable. Do not copy an executable from an unfamiliar tutorial download.

Run go mod init example.com/hello. The example module path is an identifier for this learning project, not a website you need to purchase. The command creates go.mod in the current directory. Work in a fresh folder rather than inside an unrelated repository, where a parent module or workspace could change the meaning of a command. Keep this first exercise free of configuration that has nothing to do with the behavior being learned.

2. Separate the behavior from the entry point

Create main.go with the program below. The main function is the command entry point, while greeting holds the behavior we want to test. Keeping that behavior in a small function lets a test call it without running a separate process or capturing a terminal. The blank-name case is an explicit application decision: this example greets a Gopher rather than reporting an error. A different program could choose a different contract.

package main

import "fmt"

func greeting(name string) string {
    if name == "" {
        return "Hello, Gopher!"
    }
    return "Hello, " + name + "!"
}

func main() {
    fmt.Println(greeting("Ada"))
}

Run go run . from the module folder. The expected line is Hello, Ada!. The dot selects the current package, so the command is not interchangeable with running from an arbitrary directory. If the result differs, check the directory and saved file before adding imports or changing environment settings. A useful debugging habit is to reduce the number of assumptions before increasing the size of the solution.

3. Add a test with a visible failure path

Create main_test.go in the same folder. Files ending in _test.go are discovered by the test command, and a test function follows the Test naming convention. The official testing tutorial explains this workflow in the context of a small module. The test below checks two distinct cases without a network request, a clock or a personal file path.

package main

import "testing"

func TestGreeting(t *testing.T) {
    cases := []struct {
        name string
        want string
    }{
        {"Ada", "Hello, Ada!"},
        {"", "Hello, Gopher!"},
    }
    for _, tc := range cases {
        if got := greeting(tc.name); got != tc.want {
            t.Errorf("greeting(%q) = %q; want %q", tc.name, got, tc.want)
        }
    }
}

Check that the test is real

Run go test ./..., then temporarily change one expected greeting to a different string and run the command again. The changed expectation should fail. Restore it and rerun the tests afterward. This deliberate failure checks discovery and the assertion together; a green result alone could also mean that a test was not included in the package you selected.

  • The program and test files both declare package main.
  • The test filename ends with _test.go, not test.go or _tests.go.
  • The command is run from the directory containing go.mod.
  • The expected result describes the chosen behavior, including punctuation.

4. Finish the feedback loop before expanding the project

Run go fmt ./... to format the files, go vet ./... for its static checks and go test ./... for the behavior checks. These commands answer different questions. Formatting is not a test, and a passing unit test does not prove that a larger command handles every environment or invalid input. In this exercise the function receives a string directly; argument parsing and exit statuses have not been implemented.

Document the exact commands and the expected greeting in a short README. Include the Go version used when checking the example. Another reader should be able to identify which files belong to the project and reproduce the same small result without a private setup script. Keeping the first milestone narrow makes it easier to spot where the next requirement changes the contract.

5. Choose one next requirement

A sensible next step is to read a name from a command-line argument. Decide whether a missing argument uses the default greeting or causes an error, then add a focused test for that decision. Another option is to trim surrounding whitespace, but that should also be an explicit behavior change rather than an accidental transformation. Add only one requirement at a time so that a failing test points to a small, understandable difference.

This project does not need a database, a web framework or concurrent workers. Those tools solve other problems and can be introduced when a concrete requirement calls for them. The useful outcome here is a repeatable loop: edit a small behavior, format the source, run a meaningful test and understand the observed result. That loop remains useful when the program becomes larger.

Example verification

The two source files shown above were checked with Go 1.27.4 on macOS arm64. The program printed Hello, Ada!, the tests passed, and go vet reported no issues. A deliberately incorrect expected greeting caused the test to fail, after which the correct expectation was restored. These observations apply to this small example, not to every project environment.