is this a bug in "go fmt"?

113 views
Skip to first unread message

Sandeep Kalra

unread,
Nov 11, 2022, 9:07:40 PM11/11/22
to golang-nuts
Original code:

$ cat production.go

//go:build production
// +build production
// +build linux  <<<<<< Notice This Line as my production code is only available on Linux

package main

const (
    isProductionCode = true
    isDebugCode      = false
)

$ go fmt production.go
production.go
$ cat production.go
//go:build production
// +build production

package main

const (
    isProductionCode = true
    isDebugCode      = false
)
Thanks,
Sandeep Kalra

Sandeep Kalra

unread,
Nov 11, 2022, 9:13:09 PM11/11/22
to golang-nuts
and similar issue with other style of adding tags as well

i.e.
// +build tag1,tag2
// +build tag1 tag2
etc

Sean Liao

unread,
Nov 11, 2022, 9:16:02 PM11/11/22
to golang-nuts
You'll need to provide more info if you want to report a bug:

```
main » cat main.go        
// +build production
// +build linux

package main

21:13:35 ~/tmp/testrepo0017 0:00:00
main » go fmt .
main.go

21:13:39 ~/tmp/testrepo0017 0:00:00
main » cat main.go
//go:build production && linux
// +build production,linux

package main
```
- sean


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9a4b4e5d-4c3f-469d-8cc5-6a268152cd5an%40googlegroups.com.

Andrew Harris

unread,
Nov 11, 2022, 9:18:44 PM11/11/22
to golang-nuts

Sandeep Kalra

unread,
Nov 11, 2022, 9:21:51 PM11/11/22
to golang-nuts
Hi, I will try to explain:
so, I have to run my code at 3 places : linux, windows and darwin. and On each platform there is a production and debug code.
To make this happen, i am using build tags. 
for e.g. to make build for linux in production, the script will use this:
go build --tags "linux, production"  

This is good on build side of things, but not at the go code side. As soon as I send to build process, we do fmt, lint etc, the "go fmt" removes the 2nd tag and then the symbols in go file is not found. 
Best Regards,
Sandeep

Sandeep Kalra

unread,
Nov 11, 2022, 9:27:22 PM11/11/22
to golang-nuts
probably yes - the implementation of tools for this feature. 

If you see the accepted proposal there is this example:
// +build 386 windows,amd64 windows

BTW, I am on go1.19.3.

Andrew Harris

unread,
Nov 11, 2022, 9:50:23 PM11/11/22
to golang-nuts
I think the result of the proposal explains some rewriting in .go files, at least.

I believe the conventional wisdom/best practices here are:

- Use separate, parallel files to hold platform- (or tag-) specific code, e.g:
    init_darwin.go, init_linux.go, init_windows.go

- Prefer setting environment variables GOOS, GOARCH to passing build tag arguments
   
I think these quick guides are up-to-date:
https://www.digitalocean.com/community/tutorials/building-go-applications-for-different-operating-systems-and-architectures
https://www.digitalocean.com/community/tutorials/customizing-go-binaries-with-build-tags

I hope this helps, I'm not convinced I'm not missing something :)

Sandeep Kalra

unread,
Nov 11, 2022, 9:52:17 PM11/11/22
to golang-nuts
Thanks, sorted out.

this works!
 //go:build debug || linux_debug || darwin_debug || darwin
 // +build debug linux_debug darwin_debug darwin

Reply all
Reply to author
Forward
0 new messages