Tools for 80 column split for golang

2,645 views
Skip to first unread message

Sankar

unread,
Sep 7, 2017, 12:10:43 AM9/7/17
to golang-nuts
Hi

Are there any tools available for golang to split long functions so that they can fit in 80 columns (as long as possible) ? 

For example:

```
mux.Handle("/login", handlers.LoggingHandler(os.Stderr, http.HandlerFunc(lh.LoginHandler)))
mux.Handle("/add-referrer", handlers.LoggingHandler(os.Stderr, auth.Trace(auth.IsAuthenticated(auth.IsReferrer(http.HandlerFunc(lh.AddReferrerHandler))))))
```

could become:

```
mux.Handle(
"/login",
handlers.LoggingHandler(os.Stderr, http.HandlerFunc(lh.LoginHandler)))
mux.Handle(
"/add-referrer",
handlers.LoggingHandler(
os.Stderr,
auth.Trace(
auth.IsAuthenticated(
auth.IsReferrer(http.HandlerFunc(lh.AddReferrerHandler)),
),
),
),
)
```
Google groups will make the indents in the above snippet display wrong, but you get the idea.

Are there any tools (Similar to prettier for javascript) or techniques for getting this ?

Thanks.

Sankar

as

unread,
Sep 7, 2017, 2:30:13 AM9/7/17
to golang-nuts
Gofmt is the usual tool for that

; echo 'mux.Handle("/add-referrer", handlers.LoggingHandler(os.Stderr, auth.Trace(auth.IsAuthenticated(auth.IsReferrer(http.HandlerFunc(lh.AddReferrerHandler))))))' > file
; cat file | gofmt
mux
.Handle("/add-referrer", handlers.LoggingHandler(os.Stderr, auth.Trace(auth.IsAuthenticated(auth.IsReferrer(http.HandlerFunc(lh.AddReferrerHandler))))))

Usually piping to gofmt fixes syntax. But because gofmt doesn't split by line length, it wont work. You can use a stream editor like sed to replace '(' with '(\n' and then pipe to gofmt to fix that.

cat file | edit ",x,\\(,a,\n,"  | gofmt
mux
.Handle(

       
"/add-referrer", handlers.LoggingHandler(
                os
.Stderr, auth.Trace(
                        auth
.IsAuthenticated(
                                auth
.IsReferrer(
                                        http
.HandlerFunc(

                                                lh
.AddReferrerHandler))))))

Jakob Borg

unread,
Sep 7, 2017, 2:36:04 AM9/7/17
to Sankar, golang-nuts
On 7 Sep 2017, at 06:10, Sankar <sankar.c...@gmail.com> wrote:
>
> Are there any tools available for golang to split long functions so that they can fit in 80 columns (as long as possible) ?

Don't fear longer lines, most of us are not on text mode terminals any more. :)

When it becomes *too* long it's probably hard to read due to being a too large or too complex expression, not the line length per se. My preferred solution would be to split it up with a variable or two. I don't think there is a tool for that, it requires human consideration.

//jb

Sankar P

unread,
Sep 7, 2017, 4:24:19 AM9/7/17
to Jakob Borg, golang-nuts
Even with most modern laptops, I found having 80 column limit is very useful, when we split panes and read code. May be it is just my personal preference :)

snmed

unread,
Sep 7, 2017, 8:40:08 AM9/7/17
to golang-nuts
Hi 

Typically you can break your lines after comma ,, after opening parenthesis e.g. ([{, and after a dot . which may be referencing a field or method of some value. You can also break your line after binary operators (those that require 2 operands), e.g.:

And here a simple example with fmt.Println: https://play.golang.org/p/tP8zkOo79l 

Cheers snmed

Drew Derbyshire

unread,
Sep 8, 2017, 12:27:15 PM9/8/17
to golang-nuts
I haven't edited on a true text mode terminal since 1992, but a column limit is still useful.  This and the other Go standards for relatively fixed formatting are not its strong suit.

Axel Wagner

unread,
Sep 8, 2017, 2:22:14 PM9/8/17
to Drew Derbyshire, golang-nuts
I think people are focusing on the wrong part of Jakob's reply. Focus on the "if your line is too long, it's probably a symptom of a too complicated expression" part, not the "line-length limit doesn't matter" part.

I am very rarely exceeding 80 columns. For example:

var h http.Handler

h = handlers.LoggingHandler(os.Stderr, http.HandlerFunc(lh.LoginHandler))
mux.Handle("/login", h)

h = http.HandlerFunc(lh.AddReferrerHandler)
h = auth.IsReferrer(h)
h = auth.IsAuthenticated(h)
h = auth.Trace(h)
h = handlers.LoggingHandler(os.Stderr, h)
mux.Handle("/add-referrer", h)

Personally, I find that much more readable anyway…

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Axel Wagner

unread,
Sep 8, 2017, 2:23:51 PM9/8/17
to golang-nuts
(there's also an argument in there that "handlers.LoggingHandler" should actually be called at most "handlers.Logging", but probably even "handlers.Log". And that the other names are also not great… but that's a different argument)
Reply all
Reply to author
Forward
0 new messages