Go新手求助:特定目录结构的构建、包引用与测试配置方法
Hey there! Let's walk through your Go project questions one by one—this stuff can feel a bit overwhelming when you're starting out, but once you wrap your head around packages and modules, it'll make total sense.
Looking at your setup, there's a small indentation issue with cli.go—it should live directly under the app directory, not inside the view subfolder. Here's the corrected structure you'll want:
. ├── app │ ├── controller │ │ └── (your controller files, e.g., user_controller.go) │ ├── model │ │ └── (your model files, e.g., user.go) │ ├── view │ │ └── (your view files, e.g., user_view.go) │ └── cli.go # This is your main entry point ├── go.mod └── tests ├── controller │ └── controller_test.go ├── model │ └── model_test.go └── view └── view_test.go
Packages are Go's way of organizing code—think of them as logical groups for related functionality. Here's the key stuff to remember:
- Every
.gofile starts with apackage <name>declaration that ties it to a package. - All files in the same directory must belong to the same package (this is a hard rule!).
- By convention, the package name matches the directory name (it's not required, but it keeps things clear).
- Only identifiers (functions, structs, variables) with uppercase first letters are "exported"—meaning other packages can use them. Lowercase ones are private to the package.
For your project:
- Files in
app/controllershould usepackage controller - Files in
app/modelusepackage model cli.gomust usepackage main—this tells Go it's the entry point for an executable program.
Since your module is declared as 10x.dev/app in go.mod, you'll import the controller package using its full module path. Here's what cli.go would look like:
// app/cli.go package main import ( "10x.dev/app/app/controller" ) func main() { // Call an exported function from the controller package (notice the uppercase "DoSomething") controller.DoSomething() }
Just make sure any function/struct you want to use from controller has an uppercase first letter—otherwise, it'll be private and you won't be able to access it.
go build Since cli.go is your main entry point (in the main package), you can build it from the project root (where go.mod lives) with:
go build ./app
This will create an executable named app (or app.exe on Windows) in your root directory. If you want a custom name, use the -o flag:
go build -o my_custom_app ./app
Alternatively, you can cd into the app directory and run go build directly—this will create the executable inside the app folder.
go test to Work for Your Tests Your test files in the tests directory need to import the corresponding app packages using the full module path. For example, tests/controller/controller_test.go might look like this:
// tests/controller/controller_test.go package controller_test import ( "testing" "10x.dev/app/app/controller" ) func TestDoSomething(t *testing.T) { result := controller.DoSomething() if result != "expected_value" { t.Errorf("Got %s, expected %s", result, "expected_value") } }
To run all your tests, go back to the project root and run:
go test ./tests/...
The ... tells Go to look for test files in all subdirectories of tests. If you want to run tests for just one package, use:
go test ./tests/controller
Once you're comfortable with the basics, you might want to adopt the community-standard structure—it's easier for other Go developers to understand, and it scales better:
. ├── cmd │ └── app # Main entry point lives here (rename cli.go to main.go) ├── internal # Internal packages—only your project can use these (Go blocks external imports) │ ├── controller │ ├── model │ └── view ├── pkg # Optional: For packages you want to share with other projects ├── go.mod └── (Tests can live alongside their packages, e.g., internal/controller/controller_test.go)
A common practice is to keep test files in the same directory as the code they test—this makes it easier to find and run tests for specific components.
内容的提问来源于stack exchange,提问作者belteshazzar

