Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: mention language downgrading via //go:build #61894

Open
dominikh opened this issue Aug 9, 2023 · 3 comments
Open

doc: mention language downgrading via //go:build #61894

dominikh opened this issue Aug 9, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dominikh
Copy link
Member

dominikh commented Aug 9, 2023

Reading https://go.dev/doc/toolchain, only upgrading the language version on a per-file basis is shown, not downgrading.

It does say that

The language version can be changed on a per-file basis by using a build constraint

but that's a bit implicit, considering this is a major change from how module versions and build constraints used to interact.

https://pkg.go.dev/cmd/go#hdr-Build_constraints doesn't mention the semantics of version build constraints, either.

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 9, 2023
@mknyszek mknyszek added this to the Backlog milestone Aug 9, 2023
@mknyszek
Copy link
Contributor

mknyszek commented Aug 9, 2023

CC @rsc

@oliverpool
Copy link

oliverpool commented Nov 22, 2023

I got bitten by this in #64308 where implementing a polyfill for a previous version of the compiler would not work:

//go:build !go1.21

package main

// min is a pseudo-polyfill for go1.21 min function
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Currently the doc states:

For example, a module containing code that uses the Go 1.21 language version should have a go.mod file with a go line such as go 1.21 or go 1.21.3. If a specific source file should be compiled only when using a newer Go toolchain, adding //go:build go1.22 to that source file both ensures that only Go 1.22 and newer toolchains will compile the file and also changes the language version in that file to Go 1.22.

It would be helpful to add something like:

For a module to use builtin functions introduced in a later version of Go, use a proxy function with two files implementing it. One with the //go:build go1.22 build constraint which directly calls the builtin, the other with //go:build !go1.22, with a polyfill of the (not-yet available) builtin.

@oliverpool
Copy link

oliverpool commented Nov 22, 2023

the language version declared by the go directive of the go.mod is not used to satisfy build constraints (which are checked against the currently running Go toolchain).

Edit: I just realized that this is only relevant for builtins (since stdlib or syntax changes require 2 mutually excluding Go files)

@rsc rsc modified the milestones: Backlog, Go1.22 Dec 5, 2023
@gopherbot gopherbot modified the milestones: Go1.22, Go1.23 Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

6 participants