You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

First: Fix Your Directory Structure

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
What Do Packages Do, Exactly?

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 .go file starts with a package <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/controller should use package controller
  • Files in app/model use package model
  • cli.go must use package main—this tells Go it's the entry point for an executable program.
How to Reference Controller Code in cli.go

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.

Building Your Project with 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.

Getting 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
Bonus: Moving to a Standard Go Project Structure

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:22:46