Go 如何支持 Allman 风格编码?

发布于 2024-11-15 10:48:46 字数 467 浏览 1 评论 0原文

在我用其他语言处理过的所有项目中,选择的支撑样式是 Allman 样式(又名 ANSI 样式)。在 Go 中工作时,我在其他 C 风格语法系列语言中缺少自由形式的大括号样式(也包括括号)。

谁能想出一种方法让 Go 编译器接受以下支撑样式?

package main

import "fmt"

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜巴黎 2024-11-22 10:48:46

我把牙套加倍。

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

这并不理想,但比尝试扫描第 1-120 列以查找匹配的大括号要好。

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.

高冷爸爸 2024-11-22 10:48:46

这可能不完全是您正在寻找的,但这是一种可能的方式。

您可以编写一个“翻译程序”,本质上是一个极其简单的编译器,可以将您编写的内容(实际上是 Go 变体)转换为 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.

方圜几里 2024-11-22 10:48:46

简而言之,不。该语言(或者一年或更长时间前)是用分号定义的,编译器会在行尾隐式插入分号 - 除非行尾有大括号。因此,如果您编写一个条件(不需要括号,请注意)并且不在行末尾放置左大括号,那么 Go 编译器会为您插入一个 - 在 if,大括号括起无条件执行的块。

@epw 建议重新格式化;我认为这是一个合理的建议。 Go 附带了 gofmt 来转换为规范的 Go 风格。您必须编写一些内容来从规范风格转换为 Allman 风格,反之亦然,并确保在将 Go-Allman 源代码编译为目标文件之前将其预编译为 Go 规范格式。总的来说,这比接受 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.)

风筝在阴天搁浅。 2024-11-22 10:48:46

尝试一下 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
(   "fmt"
)


func main()
{   if false
    {   fmt.Println("jack")
        fmt.Println("forkgo")
    } else
    {   fmt/
           .Println("hello")
        fmt.Println("forkgo")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文