From 3560419ce9e2c8e8d7fd8a8221cebcb588a3c715 Mon Sep 17 00:00:00 2001 From: Konstantin Shaposhnikov Date: Sat, 28 May 2016 10:59:14 +0800 Subject: [PATCH] godoc/static: makestatic.go: gofmt generated code Change-Id: Ibe0948fa283af94b1213e9ef344ef9eda6e59c05 Reviewed-on: https://go-review.googlesource.com/23532 Reviewed-by: Andrew Gerrand --- godoc/static/makestatic.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/godoc/static/makestatic.go b/godoc/static/makestatic.go index f48313c2..3886eb28 100644 --- a/godoc/static/makestatic.go +++ b/godoc/static/makestatic.go @@ -10,9 +10,9 @@ package main import ( - "bufio" "bytes" "fmt" + "go/format" "io/ioutil" "os" "unicode/utf8" @@ -83,27 +83,28 @@ func makestatic() error { return err } defer f.Close() - w := bufio.NewWriter(f) - fmt.Fprintf(w, "%v\npackage static\n\n", warning) - fmt.Fprintf(w, "var Files = map[string]string{\n") + buf := new(bytes.Buffer) + fmt.Fprintf(buf, "%v\npackage static\n\n", warning) + fmt.Fprintf(buf, "var Files = map[string]string{\n") for _, fn := range files { b, err := ioutil.ReadFile(fn) if err != nil { return err } - fmt.Fprintf(w, "\t%q: ", fn) + fmt.Fprintf(buf, "\t%q: ", fn) if utf8.Valid(b) { - fmt.Fprintf(w, "`%s`", sanitize(b)) + fmt.Fprintf(buf, "`%s`", sanitize(b)) } else { - fmt.Fprintf(w, "%q", b) + fmt.Fprintf(buf, "%q", b) } - fmt.Fprintln(w, ",\n") + fmt.Fprintln(buf, ",\n") } - fmt.Fprintln(w, "}") - if err := w.Flush(); err != nil { + fmt.Fprintln(buf, "}") + fmtbuf, err := format.Source(buf.Bytes()) + if err != nil { return err } - return f.Close() + return ioutil.WriteFile("static.go", fmtbuf, 0666) } // sanitize prepares a valid UTF-8 string as a raw string constant.