go 测试文件不应包含 main 函数,而应使用以 `test` 开头的函数定义测试用例,并与被测代码同属 `package main`;`go test` 会自动识别并执行这些测试函数。
在 Go 中编写 Web 应用(如基于 net/http 的服务)的测试时,一个常见误区是直接将官方 httptest 示例中的 main() 函数复制到 _test.go 文件中——这会导致编译失败,因为同一个包内不允许存在多个 main 函数(尤其当主程序文件 beacon.go 已定义 func main() 时)。
✅ 正确做法是:
下面是一个完整示例:
// beacon.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, Beacon!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}// beacon_test.go
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHandlerReturnsOK(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler(w, req)
if w.Code != http.StatusOK {
t.Errorf("expected status OK; got %v", w.Code)
}
expected := "Hello, Beacon!"
if w.Body.String() != expected {
t.Errorf("expected body %q; got %q", expected, w.Body.String())
}
}? 注意事项:
总结:Go 的
测试机制简洁而严格——遵循 TestXxx(*testing.T) 命名约定、保持包一致性、移除冗余 main,即可让 go test 稳健运行。这是构建可维护 Go Web 服务的基础实践。