github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/version/buildinfo.go (about)

     1  //go:build go1.12
     2  // +build go1.12
     3  
     4  package version
     5  
     6  import (
     7  	"bytes"
     8  	"runtime/debug"
     9  	"text/template"
    10  )
    11  
    12  func init() {
    13  	buildInfo = moduleBuildInfo
    14  }
    15  
    16  var buildInfoTmpl = ` mod	{{.Main.Path}}	{{.Main.Version}}	{{.Main.Sum}}
    17  {{range .Deps}} dep	{{.Path}}	{{.Version}}	{{.Sum}}{{if .Replace}}
    18  	=> {{.Replace.Path}}	{{.Replace.Version}}	{{.Replace.Sum}}{{end}}
    19  {{end}}`
    20  
    21  func moduleBuildInfo() string {
    22  	info, ok := debug.ReadBuildInfo()
    23  	if !ok {
    24  		return "not built in module mode"
    25  	}
    26  
    27  	buf := new(bytes.Buffer)
    28  	err := template.Must(template.New("buildinfo").Parse(buildInfoTmpl)).Execute(buf, info)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	return buf.String()
    33  }