gofmt as a package

276 views
Skip to first unread message

Albert Strasheim

unread,
Nov 19, 2010, 1:17:10 AM11/19/10
to golang-nuts
Hello all

I am working on a tool that generates some Go code into files.

I would like to format the code like gofmt does.

One option is to run gofmt on the generated files, but it would be
nicer if I could output properly formatted code directly from my
program.

Does anybody have any comments on the idea of moving most of the code
in the gofmt command to a package (maybe called go/format) that can be
used by gofmt and other code generators?

Regards

Albert

Jessta

unread,
Nov 19, 2010, 1:27:41 AM11/19/10
to Albert Strasheim, golang-nuts
On Fri, Nov 19, 2010 at 5:17 PM, Albert Strasheim <ful...@gmail.com> wrote:
> Does anybody have any comments on the idea of moving most of the code
> in the gofmt command to a package (maybe called go/format) that can be
> used by gofmt and other code generators?

Most of the code for gofmt is already in a package.
http://golang.org/pkg/go/printer/

- jessta
--
=====================
http://jessta.id.au

Albert Strasheim

unread,
Nov 19, 2010, 1:32:11 AM11/19/10
to golang-nuts
Hello

Thanks. After looking over the extra bits provided by gofmt again, I
think go/printer will do the trick.

Regards

Albert

On Nov 19, 8:27 am, Jessta <jes...@jessta.id.au> wrote:

Archos

unread,
Nov 19, 2010, 3:54:18 AM11/19/10
to golang-nuts
You've to use "go/parser" and "go/printer". Here[1] you can see how it
was used by me to get it.


[1]: https://github.com/kless/goheader/blob/master/cmd/format.go

Brendan Tracey

unread,
Nov 21, 2013, 6:50:02 PM11/21/13
to golan...@googlegroups.com
The link by Archos is dead. Is there an example floating around somewhere of how to call gofmt from a go program without using a system call (just for formatting code, I don't need to use the replacement tools)

Andrew Gerrand

unread,
Nov 21, 2013, 6:55:17 PM11/21/13
to Brendan Tracey, golang-nuts
You want the go/format package:

Andrew


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Brendan Tracey

unread,
Nov 21, 2013, 7:39:48 PM11/21/13
to Andrew Gerrand, golang-nuts
Thank you Andrew. I didn’t know that existed.
Reply all
Reply to author
Forward
0 new messages