Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support Allman Style coding in Go?

In all the projects I've worked with in other languages the bracing-style of choice has been the Allman Style(aka ANSI style). The lack of a free-form bracing style(parenthesis too) is something I miss from other C-style syntax family of languages when working in Go.

Can anyone come up with a way to make the Go compiler accept the following bracing-style?

package main

import "fmt"

func main() 
{
    f()
    fmt.Println("Returned normally from f.")
}

func f() 
{
    fmt.Println("In function f.")
}

Note I am aware of the arguments for why Go was designed with such artificial 'limitation', but I'm not really buying into it. I'm a firm believer that the bracing-style used should really be decided by the coding-standard adopted by the people or company working on the code-base rather than being forced upon by the language itself.

As such please consider my question within the scope of 'how it can be done' rather than 'why not to do it and just adapt'.

Thanks

like image 842
greatwolf Avatar asked Jun 11 '11 00:06

greatwolf


2 Answers

Succinctly, no. The language is (or was a year or more ago) defined with semi-colons, and the compiler inserts semi-colons implicitly at the ends of lines - unless there's a brace at the end of the line. So, if you write a condition (which doesn't need the parentheses, please note) and do not put an open brace at the end of the line, then the Go compiler inserts one for you - leaving a null statement after the if, and the braces enclosing a block that is executed unconditionally.

@epw suggests a reformatter; I think that is a reasonable suggestion. Go comes with gofmt to convert to the canonical Go style. You'd have to write something to convert from canonical to Allman style, and vice versa, and ensure that you pre-compile your Go-Allman source into Go-canonical format before compiling it to object files. On the whole, this is more traumatic than accepting that Go has its own rules which are no more eccentric than Python's rules and that it is simplest to go with the flow and accept that coding in Go involves coding in non-Allman (approximately K&R) style. (I like and use Allman style in C; I use Go-style in Go.)

like image 158
Jonathan Leffler Avatar answered Sep 29 '22 12:09

Jonathan Leffler


I double the braces up.

if x < 0 {
{
    return sqrt(-x) + "i"
}}

It's not ideal but better than trying to scan columns 1-120 for matching braces.

like image 37
Neutrino Avatar answered Sep 29 '22 11:09

Neutrino