I am a little confused about how to structure a go web app and its tests. I have read the How to Write Go Code but still don't get it. For example, I have a go project called "beacon" with a beacon.go
file at the root. Adding a trivial beacon_test.go
file (copied verbatim from http://golang.org/pkg/net/http/httptest/#example_Server) causes this error:
$ go test
# github.com/jelder/beacon
./beacon_test.go:11: main redeclared in this block
previous declaration at ./beacon.go:216
FAIL github.com/jelder/beacon [build failed]
Sure enough, line 11 is func main()
. If I instead change the package main
line in my beacon_test.go
to package hello
, I get this error instead:
can't load package: package github.com/jelder/beacon: found packages main (beacon.go) and hello (beacon_test.go) in /Users/jacob/src/github.com/jelder/beacon
beacon_test.go
has also a function called main()
rename it to TestFirst
(or any other name you like as long as it starts with Test
, note the uppercase T
is important). There is no need for that. Just run go test .
from inside the package you are working on (the one containing the *.go files). Post the full files if you need more help.