Monday, March 10, 2014

GoLang: Don't Put "Test" In Your Filename!

Still discovering weird things (or at least weird to me as a beginner) while trying to become familiar with Go.

While playing with my simple little speedtest script (I know I have a number of entries about this, but I'm spreading out my scheduled postings so it seems like I'm fixated on this much more than I really am...) I threw the source text into a file called count_speed_test.go on another machine, just to check the time it took to run on that particular configuration.

It threw a rather wonky error. I couldn't make heads or tails of it.

I studied the text, which wasn't all that complicated, and couldn't find a typo. Nada. Zip. I created a second file named countspeed.go, inserted the same text, and it compiled.

Huh?

I used md5 to check the checksums. They matched. They were the same file. The Go compiler didn't like the filename!

I sent a message to a programmer coworker dabbling in Go asking what happened; I included this text:
******
me@mymachine /go_projects/src/countspeed $ md5sum *
9a59ff21a94861590e1ecc94676db0b5  countspeed.go
9a59ff21a94861590e1ecc94676db0b5  count_speed_test.go
me@mymachine /go_projects/src/countspeed $ go build countspeed.go
me@mymachine /go_projects/src/countspeed $ ls
countspeed  countspeed.go  count_speed_test.go
me@mymachine /go_projects/src/countspeed $ go build count_speed_test.go
# command-line-arguments
runtime.main: illegal combination BL C_NONE C_NONE C_ADDR, 1 3
(2069) BL ,main.init+0(SB)
runtime.main: illegal combination BL C_NONE C_NONE C_ADDR, 1 3
(2076) BL ,main.main+0(SB)
runtime.main: illegal combination BL C_NONE C_NONE C_ADDR, 1 3
(2069) BL ,main.init+0(SB)
runtime.main: illegal combination BL C_NONE C_NONE C_ADDR, 1 3
(2076) BL ,main.main+0(SB)
main.main(0): not defined
main.init(0): not defined

me@mymachine /go_projects/src/countspeed $
************

I kind of wracked my brain trying to recall if I'd run into this on the tutorials or teaching guides or online documentation; pretty sure I hadn't. My coworker shot an email back:

"Yes, the _test file name is probably the issue. That convention is for actual tests (which you would invoke with go test)."

My suspicion was correct (Yay I could figure that much out!) and apparently the keywords I needed were "go test filename" to find the documentation. But don't count on it being apparent...I saw it was briefly touched upon, mentioned in passing, that the file name ends in _test, and most of the Go documentation focuses on the testing package and framework within the file rather than the filename itself.

In other words, don't name a Go source file with "_test" unless it's for testing your stuff. Go doesn't like it, and it's not really obvious that this is a convention. I saw my coworker in actual real life later on and I thanked him for his reply, and he added that the "test" filename thing was one of those things that is documented, but you really kinda had to know what you were looking for to find it.

Oh, the joys of a young language and the growing body of documentation not quite "there" yet...

(Although the documentation they have is remarkably good for such a young language, overall.)

No comments:

Post a Comment