ProgramGuru

Organizing Code Across Files

Start with Organizing Code Across Files

A Go package may contain several source files so that related code can be separated without forcing new packages.

What you’ll learn

  • Explain the main purpose of organizing code across files.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Files in one directory using the same package are compiled together. A function declared in helper.go can be called by code in main.go without importing the same package. For complete package builds use go run . or go build . from the appropriate module directory.

go run main.go compiles only the explicitly named file, so it can report an undefined helper when the helper lives in another file. Choose directory-level commands when learning multi-file layout.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for multi fileGo builds all matching package files for directory-level commands.main.goStep 1helper.goStep 2Same packageStep 3go run .Step 4Go builds all matching package files for directory-level commands.
Go builds all matching package files for directory-level commands.

Tools, syntax and practical notes

Two files, one package

calculator/
  go.mod
  main.go
  helper.go

main.go:

package main
import "fmt"
func main() { fmt.Println(square(6)) }

helper.go:

package main
func square(n int) int { return n * n }

From this module folder, run go run ..

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func square(number int) int { return number * number }
func main()                 { fmt.Println("Square:", square(6)) }

Output

Square: 36

Trace what the program does

This self-contained example defines the helper in the same file. The module example below places the same function in helper.go while main.go continues to call square.

Avoid common mistakes

  • Do not accidentally place package helpers in the same ordinary source directory as package main.
  • When a command names only main.go, it excludes helper.go unless you explicitly name it.

Try it yourself

Move square into helper.go, leave main in main.go, and execute go run ..

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

How do you run all source files of the current package?
Choose one answer. You can try again.

Answer and explanation

go run .

Correct. This self-contained example defines the helper in the same file.

What you should remember

You have practiced Organizing Code Across Files in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Go Modules →