github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/gover/local.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gover
     6  
     7  import (
     8  	"runtime"
     9  	"strconv"
    10  
    11  	"github.com/go-asm/go/goversion"
    12  )
    13  
    14  // TestVersion is initialized in the go command test binary
    15  // to be $TESTGO_VERSION, to allow tests to override the
    16  // go command's idea of its own version as returned by Local.
    17  var TestVersion string
    18  
    19  // Local returns the local Go version, the one implemented by this go command.
    20  func Local() string {
    21  	v, _ := local()
    22  	return v
    23  }
    24  
    25  // LocalToolchain returns the local toolchain name, the one implemented by this go command.
    26  func LocalToolchain() string {
    27  	_, t := local()
    28  	return t
    29  }
    30  
    31  func local() (goVers, toolVers string) {
    32  	toolVers = runtime.Version()
    33  	if TestVersion != "" {
    34  		toolVers = TestVersion
    35  	}
    36  	goVers = FromToolchain(toolVers)
    37  	if goVers == "" {
    38  		// Development branch. Use "Dev" version with just 1.N, no rc1 or .0 suffix.
    39  		goVers = "1." + strconv.Itoa(goversion.Version)
    40  		toolVers = "go" + goVers
    41  	}
    42  	return goVers, toolVers
    43  }