go build of a *_unix.go file still used under windows ?

554 views
Skip to first unread message

Peter Galbavy

unread,
Aug 31, 2023, 3:57:15 AM8/31/23
to golang-nuts
I have used comment level build constraints for a while but I moved to using _linux.go and _windows.go over time as it makes it clearer for external viewing.

A user reported they tried to build on MacOS and of course this didn't work. I renamed the _linux.go files to _unix.go suffixes but then when I do "GOOS=windows go build" those files were pulled in too. While I would say Windows is a POSIX-ish system I would contend it's not UNIX :-)

I have gone and added "//go:build !windows" to all the renamed files for now, but is this intended behaviour?

Peter

Brian Candler

unread,
Aug 31, 2023, 5:13:12 AM8/31/23
to golang-nuts

Peter Galbavy

unread,
Aug 31, 2023, 5:44:14 AM8/31/23
to golang-nuts
Ah! Got it. Thanks.

Would it perhaps be good to mention this in the docs, here: https://pkg.go.dev/cmd/go#hdr-Build_constraints ?

The bullet points refer to "unix" (which is true) and then a naïve reader, like me, would think the later paragraphs about GOOS also apply.

Peter 

Brian Candler

unread,
Aug 31, 2023, 6:11:06 AM8/31/23
to golang-nuts

During a particular build, the following build tags are satisfied:

  • ...
  • "unix", if GOOS is a Unix or Unix-like system.
  • ...

It then says:

If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS *_GOARCH *_GOOS_GOARCH

(example: source_windows_amd64.go) where GOOS and GOARCH represent any known operating system and architecture values respectively, then the file is considered to have an implicit build constraint requiring those terms (in addition to any explicit constraints in the file).


This says that only GOOS and GOARCH are considered; it doesn't say that all possible build tags are interpreted when in the filename, and indeed there are many that are not.  For example, "foo_gccgo.go" and "foo_go1.12.go" are not treated specially. So I think it's fairly clear here, as long as you accept that "unix" is neither a GOOS nor a GOARCH.

Aside: finding a comprehensive list of GOOS and GOARCH values is a bit tricker. You can run "go tool dist list", or there are third-party summaries like

Peter Galbavy

unread,
Sep 1, 2023, 3:49:20 AM9/1/23
to golang-nuts
After your initial reply and carefully re-reading I also agree that's what it means. It is however, IMHO, not clear on first reading and there is an implication by omission that "unix" could be considered a valid "GOOS" value. Now I know, I know. The next first-time reader, perhaps not so much.

Peter

Reply all
Reply to author
Forward
0 new messages