make flagValueIter an iter.Seq

This is purely a cosmetic change, to make the callers nicer.
This commit is contained in:
Daniel Martí
2026-04-14 01:08:52 +02:00
committed by Paul
parent 2e5692934c
commit 1186bca36d
2 changed files with 29 additions and 22 deletions
+20 -13
View File
@@ -17,6 +17,7 @@ import (
"go/version"
"io"
"io/fs"
"iter"
"log"
"os"
"os/exec"
@@ -679,25 +680,31 @@ func splitFlagsFromFiles(all []string, ext string) (flags, paths []string) {
// the last value is returned.
func flagValue(flags []string, name string) string {
lastVal := ""
flagValueIter(flags, name, func(val string) {
for val := range flagValues(flags, name) {
lastVal = val
})
}
return lastVal
}
// flagValueIter retrieves all the values for a flag such as "-foo", like
// flagValues retrieves all the values for a flag such as "-foo", like
// flagValue. The difference is that it allows handling complex flags, such as
// those whose values compose a list.
func flagValueIter(flags []string, name string, fn func(string)) {
for i, arg := range flags {
if val, ok := strings.CutPrefix(arg, name+"="); ok {
// -name=value
fn(val)
}
if arg == name { // -name ...
if i+1 < len(flags) {
// -name value
fn(flags[i+1])
func flagValues(flags []string, name string) iter.Seq[string] {
return func(yield func(string) bool) {
for i, arg := range flags {
if val, ok := strings.CutPrefix(arg, name+"="); ok {
// -name=value
if !yield(val) {
return
}
}
if arg == name { // -name ...
if i+1 < len(flags) {
// -name value
if !yield(flags[i+1]) {
return
}
}
}
}
}
+9 -9
View File
@@ -56,11 +56,11 @@ func computeLinkerVariableStrings(pkg *types.Package) (map[*types.Var]string, er
if err != nil {
return nil, err
}
flagValueIter(ldflags, "-X", func(val string) {
for val := range flagValues(ldflags, "-X") {
// val is in the form of "foo.com/bar.name=value".
fullName, stringValue, found := strings.Cut(val, "=")
if !found {
return // invalid
continue // invalid
}
// fullName is "foo.com/bar.name"
@@ -69,15 +69,15 @@ func computeLinkerVariableStrings(pkg *types.Package) (map[*types.Var]string, er
// Note that package main always has import path "main" as part of a build.
if path != pkg.Path() && (path != "main" || pkg.Name() != "main") {
return // not the current package
continue // not the current package
}
obj, _ := pkg.Scope().Lookup(name).(*types.Var)
if obj == nil {
return // no such variable; skip
continue // no such variable; skip
}
linkerVariableStrings[obj] = stringValue
})
}
return linkerVariableStrings, nil
}
@@ -1329,11 +1329,11 @@ func (tf *transformer) transformLink(args []string) ([]string, error) {
// Make sure -X works with obfuscated identifiers.
// To cover both obfuscated and non-obfuscated names,
// duplicate each flag with a obfuscated version.
flagValueIter(flags, "-X", func(val string) {
for val := range flagValues(flags, "-X") {
// val is in the form of "foo.com/bar.name=value".
fullName, stringValue, found := strings.Cut(val, "=")
if !found {
return // invalid
continue // invalid
}
// fullName is "foo.com/bar.name"
@@ -1350,11 +1350,11 @@ func (tf *transformer) transformLink(args []string) ([]string, error) {
// We couldn't find the package.
// Perhaps a typo, perhaps not part of the build.
// cmd/link ignores those, so we should too.
return
continue
}
newName := hashWithPackage(lpkg, name)
flags = append(flags, fmt.Sprintf("-X=%s.%s=%s", lpkg.obfuscatedImportPath(), newName, stringValue))
})
}
// Starting in Go 1.17, Go's version is implicitly injected by the linker.
// It's the same method as -X, so we can override it with an extra flag.