Published on

Go Spreading Long Code Across Lines

Authors

Today I was busy working on a piece of code that had many chainable methods. This is really cool as you do not have to keep reassigning and calling a method on structs but I could not workout how to separate this across multiple lines. I basically had code similar to the below:

someStructWithChainableMethods.method1().method2().method3()

Normally in most languages you would put this on separate lines as follows:

someStructWithChainableMethods
    .method1()
    .method2()
    .method3()

But this does not work.

After doing a quick search I came across this answer. Long story short as you can put a new line after the following characters: (,[,{,, and .. So the key is you add your newlines after these characters so the above would become:

someStructWithChainableMethods.
    method1().
    method2().
    method3()