golang tool for splitting long lines

281 views
Skip to first unread message

Sankar

unread,
Nov 15, 2019, 12:17:29 AM11/15/19
to golang-nuts
Hi

In Javascript world I have this handy tool Prettier which will automatically, reproducibly break long lines of code into multiple lines (and also merge parameters into a single line if some elements are removed). 

Are there similar tools available for Golang ? Either as a standalone program or as VSCode/Goland plugins.

From https://github.com/golang/go/issues/11915 I believe that go team may not address it as natively as gofmt/goimports. But are there any other hacks/tools that people already use to break long lines ? 

Thanks.

Sankar

anderson...@blacklane.com

unread,
Nov 18, 2019, 4:29:28 AM11/18/19
to golang-nuts
Hi Sankar,

Not really, a easy trick is to use json.MarshalIndent. It helped me a lot :)

playgound: https://play.golang.org/p/nfr2ANR6pPH

type A struct {
    A
int
    B
string
    C map
[int]string
}

func main
() {
    aa
:= A{
        A
: 1,
        B
: "2",
        C
: map[int]string{3: "3"},
   
}

    bs
, _ := json.MarshalIndent(aa, "", "    ")
    fmt
.Printf("%#v\n", aa)
    fmt
.Println(string(bs))

}

Bill Nixon

unread,
Nov 24, 2019, 10:22:05 AM11/24/19
to golang-nuts
I agree json.MarshalIndent is great. However, be aware that json.MarshalIndent will not output private (lowercase) fields of the struct.
Reply all
Reply to author
Forward
0 new messages