Re: code review 6852075: go/fmt: fmt implements standard formatting of Go source (issue 6852075)

57 views
Skip to first unread message

g...@golang.org

unread,
Nov 20, 2012, 9:18:58 PM11/20/12
to r...@golang.org, brad...@golang.org, da...@cheney.net, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com

Robert Griesemer

unread,
Nov 20, 2012, 9:20:06 PM11/20/12
to Rob Pike, Brad Fitzpatrick, Dave Cheney, golang-dev, re...@codereview-hr.appspotmail.com
PS: Better (more usable, simpler) API suggestions welcome.
- gri

brad...@golang.org

unread,
Nov 20, 2012, 10:27:14 PM11/20/12
to g...@golang.org, r...@golang.org, da...@cheney.net, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com

https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go
File src/pkg/go/fmt/fmt.go (right):

https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go#newcode32
src/pkg/go/fmt/fmt.go:32: return (&printer.Config{Mode: printerMode,
Tabwidth: tabWidth}).Fprint(dst, fset, node)
this printer.Config could be a global variable.

https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go#newcode38
src/pkg/go/fmt/fmt.go:38: func String(src string) (string, error) {
Could also make src be of type interface{}, like parser.ParseFile, which
takes []byte, string, or io.Reader.

https://codereview.appspot.com/6852075/

r...@golang.org

unread,
Nov 20, 2012, 10:42:27 PM11/20/12
to g...@golang.org, brad...@golang.org, da...@cheney.net, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go#newcode6
src/pkg/go/fmt/fmt.go:6: package fmt
s/surce/source/
also in CL description

this should be package format, not fmt. it will require a renamed import
almost everywhere it's used.

https://codereview.appspot.com/6852075/

rogp...@gmail.com

unread,
Nov 21, 2012, 10:01:39 AM11/21/12
to g...@golang.org, r...@golang.org, brad...@golang.org, da...@cheney.net, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
nice.
https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go#newcode36
src/pkg/go/fmt/fmt.go:36: // an error. src is expected to be a
syntactically correct Go source file.
It would be more useful if this implemented the same heuristics that
gofmt does to allow formatting of more kinds of program elements.

https://codereview.appspot.com/6852075/diff/9001/src/pkg/go/fmt/fmt.go#newcode38
src/pkg/go/fmt/fmt.go:38: func String(src string) (string, error) {
On 2012/11/21 03:27:14, bradfitz wrote:
> Could also make src be of type interface{}, like parser.ParseFile,
which takes
> []byte, string, or io.Reader.

personally I'm not keen on that kind of thing. given that ParseFile
always reads into a []byte anyway, perhaps just:

func Write(dst io.Writer, source []byte) error

might be good enough to replace both String and Copy here.
the following doesn't seem too much worse than format.String(s)
to me:

var b bytes.Buffer
err := format.Write(&b, []byte(s))
use(b.String())

doesn't seem too much worse than using format.String
to me.

The single entry point makes it obvious what the trade-offs are
too.

https://codereview.appspot.com/6852075/
Reply all
Reply to author
Forward
0 new messages