I have a todo CLI app with the following structure
tree .
.
|-- cmd
| `-- todo
| |-- main.go
| `-- main_test.go
|-- go.mod
|-- todo.go
|-- todo.json
`-- todo_test.go
In the main.go, I have a function called expand, it expand range string like in the cut command
When I call the expand function from main_test.go, I get the following error
cmd/todo/main_test.go:141:12: undefined: expand
FAIL PACKAGE_URI/todo/cmd/todo [build failed]
FAIL
I tried to change the function visibility, but this doesn’t help either. I replicated the dir structure on a dummy project, and it did work!!
edit: Add repo uri


Your specific question is answered here: https://www.sobyte.net/post/2023-04/go-test-path-main/.
Generally this is easily solved by moving the code you are testing to a package for example
pkg/todo.goandpkg/todo_test.goor utility or whatever.Finally if you look at that link i sent they recommend not testing main in exactly the way you are testing it. One negative of the way you are testing it is you aren’t really unit testing – every time you run main_test you compile main, this is a little unusual.
Again, thank you; this is very insightful. problem solved