4 ms·
> if I name my source file i_love_linux.go, it won’t get compiled on my Mac What the fuck?
by xigoi 4y ago
> if I name my source file i_love_linux.go, it won’t get compiled on my Mac
What the fuck?
- tanduv 4y agoExplanation from a previous discussion - https://news.ycombinator.com/item?id=16414435 https://news.ycombinator.com/item?id=16414435 ...seriously wtf
- justanorherhack 4y agoIn 5 years of writing to code this has never prevented me from naming a file what I wanted to name it. I enforced conventions are nice, when I need to look at what’s different between platforms I can very quickly do so in any large repo.
- tandr 4y agoThere some file name conventions in place in Go [1]. Files ending in *_test.go are going to be run dufing `go test ` invocation, but not compiled during production build run. In general, last parts of file name reflect "tags", or a platform that this file should be built for. So, in the case of *_linux.go file will only be compiled when it targeted linux platform. Allows to have a file say file_windows_amd64.go and file_linux.go that have functions with same signature, but only one will be picked up for a target platform. Sort of like #ifdef , but at file-name level. There is a way to specify what file is actually targeting inside the file, through //go: "pragmas" too [1] https://stackoverflow.com/questions/25161774/what-are-conventions-for-filenames-in-go https://stackoverflow.com/questions/25161774/what-are-conven...
- quickthrower2 4y agoSearching around a few blogs mention _linux.go files are only compiled on Linux. But I see nothing in the official documentation, will at least in my quick searches. It will catch someone out who uses BSD as an acronym for something in their domain model (Bulk Sales Discount?) then xyz_bsd.go doesn't compile.
- j16sdiz 4y agohttps://pkg.go.dev/cmd/go#hdr-Build_constraints https://pkg.go.dev/cmd/go#hdr-Build_constraints 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).
- morelisp 4y agoIdiomatically in Go that file would be named `xyz-bsd.go`. All special filename behavior is triggered by `_suffix`.
- deleted 4y ago[deleted]
- pjmlp 4y agoOthers already explained why. Now a bit more background, this goes back to C and C++, and is considered a best practice to name translation units as name_os_arch or similar pattern, with the header file being only name, instead of #ifdef spaghetti. This is actually one of the few things I think Go did right.
- bamboozled 4y agoYeah, "If I do <some obscure thing> in <chose any language>, and it doesn't work how I like, I don't think it's good"
- xigoi 4y agoNaming a file is an obscure thing?