Skip to content

proposal: Go 2: add elixir-like pipe operator #33361

Closed
@BourgeoisBear

Description

@BourgeoisBear

I think Elixir's Pipe Operator would be a slam-dunk for Go.

Without a pipe operator, we write things like this:

s := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(x)), "/old/", "/new")

// or this:

http.HandleFunc("/",
    RequireAuthMiddleware(
        SomeOtherMiddleware(
            LogMiddleware(IndexHandler))))

Two complaints: 1) fussy parenthesis accounting, 2) text reads L-to-R, action happens R-to-L.

With a pipe operator, they could be re-written like this:

s := strings.TrimSpace(x) |> strings.ToLower() |> strings.ReplaceAll("/old/", "/new")

http.HandleFunc("/",
   LogMiddleware(IndexHandler) |> SomeOtherMiddleware() |> RequireAuthMiddleware(),
)

It's a tad longer, but easier to read. The operations have more visual separation, and now both text and action flow left-to-right.

Elixir's pipe operator only passes the first parameter. For Go, it would probably make more sense to pipe as many parameters as the previous call returns.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions