如何在Go中支持Allman风格的编码?

huangapple go评论93阅读模式
英文:

How to support Allman Style coding in Go?

问题

在我使用其他语言的所有项目中,我选择的大括号风格一直是Allman风格(也称为ANSI风格)。在使用Go语言时,我会想念其他C风格语法系列语言中的自由格式大括号风格(包括括号)。有没有人能想出一种方法让Go编译器接受以下大括号风格?

请注意,我知道为什么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

答案1

得分: 9

我将花括号加倍。

<!-- language: lang-go -->

如果 x < 0 {
{
返回 sqrt(-x) + "i"
}}

这不是理想的解决方法,但比尝试扫描1-120列以匹配花括号要好。

英文:

I double the braces up.

<!-- language: lang-go -->

if x &lt; 0 {
{
	return sqrt(-x) + &quot;i&quot;
}}

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

答案2

得分: 7

这可能不是你要找的准确方法,但这是一种可能的方式。

你可以编写一个“翻译程序”,实际上是一个非常简单的编译器,将你写的代码转换成Go编译器所期望的代码。

你可以使用类似于程序或者shell脚本的东西,将正则表达式's/(\n)$^\s*{/{\1/g'应用于整个程序(尽管它需要查看文件的完整字符串而不是逐行分割,所以你不能只将该表达式作为参数传递给sed,例如)。然后将转换后的文件写入临时文件,并在其上运行Go编译器。

这种方法的优点是不需要对Go本身进行任何更改,但缺点是你的文件将无法在没有额外脚本的情况下编译。如果你通常使用Makefile,这可能没问题,但有时可能仍然不方便。

英文:

This may not be exactly what you are looking for, but it is one possible way.

You could write a 'translator program,' essentially an incredibly simple compiler that converts from what you wrote, effectively a Go variant, to what the Go compiler itself expects.

You could do that with something along the lines of a program, even shell script, that applies the regular expression 's/(\n)$^\s*{/{\1/g' to the entire program (though it would need to look at the full string of the file and not break it up line-by-line, so you couldn't just pass that expression as an argument to sed, for example). Then write the converted file out to a temporary one, and run the Go compiler on it.

This method has the advantage of not requiring any changes to Go itself, though the disadvantage is that your files will not compile without your extra script. If you normally use a Makefile, this is probably fine, but sometimes could still be inconvenient.

答案3

得分: 6

简洁地说,不行。这种语言(或者说是一年多以前)是用分号定义的,编译器会在行尾隐式地插入分号,除非行尾有一个大括号。所以,如果你写一个条件(请注意,不需要括号),并且在行尾没有放一个大括号,那么Go编译器会为你插入一个大括号,留下一个空语句在if之后,并且大括号包围着一个无条件执行的代码块。

@epw建议使用一个重新格式化工具;我认为这是一个合理的建议。Go自带了gofmt来将代码转换为规范的Go风格。你需要编写一些代码来将规范的风格转换为Allman风格,反之亦然,并且确保在编译Go-Allman源代码之前将其预编译为Go-canonical格式。总的来说,这比接受Go有自己的规则更具有创伤性,这些规则并不比Python的规则更古怪,最简单的方法是跟随潮流,接受在Go中编码意味着以非Allman(大致上是K&R)风格编码。(我喜欢并且在C中使用Allman风格;在Go中我使用Go风格。)

英文:

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.)

答案4

得分: 2

尝试一下 https://gofork.org

forkgo 支持 allman/horstmann 风格:

package main
import
(   "fmt"
)


func main()
{   if false
    {   fmt.Println("jack")
        fmt.Println("forkgo")
    } else
    {   fmt/
           .Println("hello")
        fmt.Println("forkgo")
    }
}
英文:

Give a try to https://gofork.org

forkgo supports allman/horstmann style:

package main
import
(   &quot;fmt&quot;
)


func main()
{   if false
    {   fmt.Println(&quot;jack&quot;)
        fmt.Println(&quot;forkgo&quot;)
    } else
    {   fmt/
           .Println(&quot;hello&quot;)
        fmt.Println(&quot;forkgo&quot;)
    }
}

huangapple
  • 本文由 发表于 2011年6月11日 08:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/6313239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定