#Chaining method call in go

1 messages Β· Page 1 of 1 (latest)

rustic token
#

Hello,
Nothing really blocking here but i was wondering if something like that is ok in go :

_, err = client.
    Container().
    From("golang:1.19.1-alpine3.16").
    WithMountedDirectory("/go/src/hello", workdir).
    WithWorkdir("/go/src/hello").
    Exec(execOpts).
    Stdout().
    Contents(ctx)

Since with the go sdk there is a lot of chaining, looks better imo than
_, err = client.Container().From("golang:1.19.1-alpine3.16").WithMountedDirectory("/go/src/hello", workdir).WithWorkdir("/go/src/hello").Exec(execOpts).Stdout().Contents(ctx)

pure rivet
#

Do you mean the newlines for readability?

#

If so, yes I think that works fine. If it builds, then it means that it works πŸ™‚

rustic token
#

Never saw this anywhere so i prefered to ask πŸ™‚
Yes since it builds, everything is fine ^^'

unborn ocean
high sequoia
#

Think how good this would be with a functional language like Elixir πŸ™‚

#

@rustic token these kind of "fluent APIs" are quite common our of Go for the readability of the code. I'd encourage you to our the dot as a prefix on each line rather than at the end, as it keeps it very clear what's happening.

rustic token
#

@high sequoia Actually i did try putting the point as a prefix on the line but it surprisingly does not work πŸ˜…

high sequoia
#

Oh, Go doesn't like that πŸ˜‚

#

Sorry 😦

rustic token
#

Go doing things like no other. Not this surprising finally πŸ˜†

scenic fern
#

I use golines to do this formatting automatically. Helps those line breaks for long chained methods. Much more readable 🫑