简体   繁体   中英

How can I use gofmt to align parameters and assignment?

In a go HTTP server I'm maintaining, using VS Code, as my IDE - the gofmt command is executed whenever the file is saved but it doesn't respect manual alignment that I introduce the sake of readability.

For example, I'll have my code like this:

subRouter.Handle("/"                    , Foobar.NewAuthHandler(http.HandlerFunc(handleGetNamespaces ))).Methods("GET")
subRouter.Handle("/{namespace}"         , Foobar.NewAuthHandler(http.HandlerFunc(handleGetKeys       ))).Methods("GET")
subRouter.Handle("/{namespace}"         , Foobar.NewAuthHandler(http.HandlerFunc(handleClearNamespace))).Methods("DELETE")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleGetObject     ))).Methods("GET")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleTouchObject   ))).Methods("HEAD")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handlePutObject     ))).Methods("PUT")
subRouter.Handle("/{namespace}"         , Foobar.NewAuthHandler(http.HandlerFunc(handlePostObject    ))).Methods("POST")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleDeleteObject  ))).Methods("DELETE")

...but gofmt will compress it to this:

subRouter.Handle("/", Foobar.NewAuthHandler(http.HandlerFunc(handleGetNamespaces))).Methods("GET")
subRouter.Handle("/{namespace}", Foobar.NewAuthHandler(http.HandlerFunc(handleGetKeys))).Methods("GET")
subRouter.Handle("/{namespace}", Foobar.NewAuthHandler(http.HandlerFunc(handleClearNamespace))).Methods("DELETE")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleGetObject))).Methods("GET")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleTouchObject))).Methods("HEAD")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handlePutObject))).Methods("PUT")
subRouter.Handle("/{namespace}", Foobar.NewAuthHandler(http.HandlerFunc(handlePostObject))).Methods("POST")
subRouter.Handle("/{namespace}/{objKey}", Foobar.NewAuthHandler(http.HandlerFunc(handleDeleteObject))).Methods("DELETE")

I couldn't see any rule or option for gofmt to turn off this particular formatting rule. And documentation is sparse .

Interesting that it does format maps this way:

m := map[string]http.HandlerFunc{
        "/":            Foobar.NewAuthHandler(http.HandlerFunc(handleGetNamespaces)),
        "/{namespace}": Foobar.NewAuthHandler(http.HandlerFunc(handleGetNamespaces)),
    }

that would be one way to present your routes in a nicer format (then run through map and add them to router).

Alternatively if you wish to preserve this space, you can make a sacrifice of stars and slashes to the space eater :

subRouter.Handle("/" /*             */, Foobar.NewAuthHandler(http.HandlerFunc(handleGetNamespaces))).Methods("GET")
subRouter.Handle("/{namespace}" /*  */, Foobar.NewAuthHandler(http.HandlerFunc(handleGetKeys))).Methods("GET")

Or just accept the same colour of bikeshed as everyone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM