How to use build tag that only build on Go 1.3

Hi all!

I encounter a problem that I want to make my Go app behave differently between Go 1.4 and lower than Go 1.4.

I tried // +build !go1.4 but it does not seem to work.

Is it totally impossible or just I did it wrong?

Thanks!

@matt I found what did I do wrong right after I post, how can I delete this post … thanks…

You cannot delete it, but it would be awesome if you could write up your solution so others can learn from it.

Thanks

Dave

Make sense.

The mistake I made is:

// +build !go1.4
package xxx

Where should really have an empty line between these two lines:

// +build !go1.4

package xxx

You might need to do more than this, as this code will build on any version of Go that is no 1.4, for example, 1.2, 1.5, and all versions after that.

You probably want

// +build go1.3

package xxx

Thanks,

but isn’t go1.3 meaning build for go 1.3, 1.4, 1.5, etc?

I have two files, one has go1.4 and another one has !go1.4, and I’m using Go 1.5.1 and seems to work.

Nope, sorry.

Hmm, it might be even more confusing. I was wrong above.

For Go 1.5, it will issue the tags, go1.0, go1.1, go1.2, go1.3, go1.4, go1.5 so

// +build go1.3

will match go1.3 or later. I am sorry, you were correct before

But

// +build !go1.3

I think, but cannot test at the moment, build on Go 1.2 and lower.

build on Go 1.2 and lower.

Is it better to add !go1.0 !go1.1 !go1.2 as well?

I think you have to, however the syntax for negations is complicated, I think you have to write

// +build !go1.0
// +build !go1.1
// +build !go1.2

package p

^ untested

Got it… :joy: thanks!

What about

// +build go1.3 !go1.4

?

1 Like

Btw, go vet detects these kind of errors :slight_smile:

1 Like

Nice tip! Since my project passed the go vet, I figure I’m OK now :smiley:

I’ve just merged a new PR to vim-go which also highlights only valid build tags. Check it out: https://twitter.com/ftharsln/status/665959753609252864 You need to pull the latest master :slight_smile:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.