@@ -16,37 +16,31 @@ import (
1616 "sync"
1717)
1818
19- var (
20- stdlibPkgfileMap map [string ]string
21- stdlibPkgfileErr error
22- once sync.Once
23- )
24-
2519// PkgfileMap returns a map of package paths to the location on disk
2620// of the .a file for the package.
2721// The caller must not modify the map.
2822func PkgfileMap () (map [string ]string , error ) {
29- once .Do (func () {
30- m := make (map [string ]string )
31- output , err := exec .Command ("go" , "list" , "-export" , "-e" , "-f" , "{{.ImportPath}} {{.Export}}" , "std" , "cmd" ).Output ()
32- if err != nil {
33- stdlibPkgfileErr = err
23+ return pkgFileMapOnce ()
24+ }
25+
26+ var pkgFileMapOnce = sync .OnceValues (func () (map [string ]string , error ) {
27+ m := make (map [string ]string )
28+ output , err := exec .Command ("go" , "list" , "-export" , "-e" , "-f" , "{{.ImportPath}} {{.Export}}" , "std" , "cmd" ).Output ()
29+ if err != nil {
30+ return m , err
31+ }
32+ for line := range strings .SplitSeq (string (output ), "\n " ) {
33+ if line == "" {
34+ continue
3435 }
35- for line := range strings .SplitSeq (string (output ), "\n " ) {
36- if line == "" {
37- continue
38- }
39- sp := strings .SplitN (line , " " , 2 )
40- if len (sp ) != 2 {
41- err = fmt .Errorf ("determining pkgfile map: invalid line in go list output: %q" , line )
42- return
43- }
44- importPath , export := sp [0 ], sp [1 ]
45- if export != "" {
46- m [importPath ] = export
47- }
36+ sp := strings .SplitN (line , " " , 2 )
37+ if len (sp ) != 2 {
38+ return m , fmt .Errorf ("determining pkgfile map: invalid line in go list output: %q" , line )
4839 }
49- stdlibPkgfileMap = m
50- })
51- return stdlibPkgfileMap , stdlibPkgfileErr
52- }
40+ importPath , export := sp [0 ], sp [1 ]
41+ if export != "" {
42+ m [importPath ] = export
43+ }
44+ }
45+ return m , nil
46+ })
0 commit comments