Store test data files in a testdata directory relative to your test file, then load them using os.ReadFile with filepath.Join and filepath.Dir to ensure portability.
package mypkg
import (
"os"
"path/filepath"
"testing"
)
func TestExample(t *testing.T) {
// Locate testdata relative to this test file
dir := filepath.Dir("testdata")
data, err := os.ReadFile(filepath.Join(dir, "testdata", "input.txt"))
if err != nil {
t.Fatalf("failed to read testdata: %v", err)
}
// Use data...
}
Note: The go test command automatically includes files in a testdata directory when running tests, so you do not need to add them to your build manually.