Reviewers: rsc,
Message:
Hello
r...@golang.org (cc:
golan...@googlegroups.com),
I'd like you to review this change to
https://go.googlecode.com/hg/
Description:
cmd/go: add list template function.
It's common to use the go list command in shell scripts, but
currently it's awkward to print a string slice from the Package
type in a way that's easily parseable by the shell. For example:
go list -f '{{range .Deps}}{{.}}
{{end}}'
(and even that prints an unwanted new line at the end|).
As a possibility for making this easier, this CL adds a "list"
function to the format template, to print items separated by new lines.
For example:
go list -f '{{list .Deps}}'
This CL is mainly for discussion. There are plenty of other ways
of doing a similar thing, of course.
Please review this at
http://codereview.appspot.com/6680044/
Affected files:
M src/cmd/go/list.go
Index: src/cmd/go/list.go
===================================================================
--- a/src/cmd/go/list.go
+++ b/src/cmd/go/list.go
@@ -9,6 +9,7 @@
"encoding/json"
"io"
"os"
+ "strings"
"text/template"
)
@@ -26,8 +27,10 @@
The -f flag specifies an alternate format for the list,
using the syntax of package template. The default output
-is equivalent to -f '{{.ImportPath}}'. The struct
-being passed to the template is:
+is equivalent to -f '{{.ImportPath}}'. One extra template function
+is available, "list", which takes a slice of strings and returns
+a single string with all the slice elements separated by new lines.
+The struct being passed to the template is:
type Package struct {
Dir string // directory containing package sources
@@ -113,7 +116,8 @@
out.Write(nl)
}
} else {
- tmpl, err := template.New("main").Parse(*listFmt)
+ list := func(x []string) string { return strings.Join(x, "\n") }
+ tmpl, err := template.New("main").Funcs(template.FuncMap{"list":
list}).Parse(*listFmt)
if err != nil {
fatalf("%s", err)
}