-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go languageProposalProposal-FinalCommentPeriodv2An incompatible library changeAn incompatible library change
Milestone
Description
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.
napnie, meox, nhooyr, grandEarshot, nvkv and 13 morecholeraehyq, beoran, OneOfOne, lucy, ksshannon and 16 more
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeLanguageChangeSuggested changes to the Go languageSuggested changes to the Go languageProposalProposal-FinalCommentPeriodv2An incompatible library changeAn incompatible library change