如何在 Golang 中打破一长行代码? [英] How to break a long line of code in Golang?

查看:46
本文介绍了如何在 Golang 中打破一长行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Python,我不习惯看到超过 80 列的代码行.所以当我遇到这个:

Coming from Python, I'm not used to see code lines longer than 80 columns. So when I encounter this:

err := database.QueryRow("select * from users where user_id=?", id).Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email)

我试图打破它

err := database.QueryRow("select * from users where user_id=?", id) 
    .Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email)

但是我明白了

 syntax error: unexpected 

我也尝试过按回车键打破行并在最后放一个分号:

I also tried just breaking the line with hitting enter and put a semicolon at the end:

err := database.QueryRow("select * from users where user_id=?", id) 
.Scan(&ReadUser.ID, &ReadUser.Name, &ReadUser.First, &ReadUser.Last, &ReadUser.Email);

但我再次得到:

syntax error: unexpected .

所以我想知道 golangic 的方法是什么?

So I'm wondering what's the golangic way to do so?

推荐答案

首先介绍一下背景.Go 的正式语法在许多产生式中使用分号 ";" 作为终止符,但是 Go 程序可能会省略其中的大部分(并且它们应该有一个更清晰、易于阅读的源代码;gofmtcode> 还删除了不必要的分号).

First some background. The formal grammar of Go uses semicolons ";" as terminators in many productions, but Go programs may omit most of them (and they should to have a clearer, easily readable source; gofmt also removes unnecessary semicolons).

规范列出了确切的规则.规范:分号:

The specification lists the exact rules. Spec: Semicolons:

正式语法使用分号;"作为许多作品的终结者.Go 程序可能会使用以下两条规则省略大部分分号:

The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

  1. 当输入被分解成标记时,如果该标记是,则分号会立即自动插入到标记流中的行的最后标记之后

  1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream immediately after a line's final token if that token is

  • an identifier
  • an integer, floating-point, imaginary, rune, or string literal
  • one of the keywords break, continue, fallthrough, or return
  • one of the operators and delimiters ++, --, ), ], or }

为了让复杂的语句占据一行,可以在结束)"或}"之前省略分号.

To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

所以你可以看到如果在括号 ) 后面插入换行符,分号 ; 将被自动插入,所以下一行不会被视为上一行的延续.这就是您的情况,因此以 .Scan(&ReadUser.ID,... 开头的下一行会给您一个编译时错误,因为这本身(没有前一行)是一个编译时错误:syntax error: unexpected .

So as you can see if you insert a newline character after the parenthesis ), a semicolon ; will be inserted automatically and so the next line will not be treated as the continuation of the previous line. This is what happened in your case, and so the next line starting with .Scan(&ReadUser.ID,... will give you a compile-time error as this standing by itself (without the previous line) is a compile-time error: syntax error: unexpected .

因此,您可以在不与上面 1. 点列出的规则冲突的任何点断线.

So you may break your line at any point which does not conflict with the rules listed under point 1. above.

通常,您可以在逗号 , 后,opening 括号后换行,例如(, [, {, and after a dot . 可能引用某个值的字段或方法. 您也可以在二元运算符(需要 2 个操作数的那些)之后换行,例如:

Typically you can break your lines after comma ,, after opening parenthesis e.g. (, [, {, and after a dot . which may be referencing a field or method of some value. You can also break your line after binary operators (those that require 2 operands), e.g.:

i := 1 +
        2
fmt.Println(i) // Prints 3

这里值得注意的一点是,如果您有一个列出初始值的结构体或切片或映射文字,并且您想在列出最后一个值后换行,则必须放置一个强制性逗号 ,即使这是最后一个值,不会有更多的值,例如:

One thing worth noting here is that if you have a struct or slice or map literal listing the initial values, and you want to break line after listing the last value, you have to put a mandatory comma , even though this is the last value and no more will follow, e.g.:

s := []int {
    1, 2, 3,
    4, 5, 6,  // Note it ends with a comma
}

这是为了符合分号规则,也让你可以重新排列和添加新行而不必关心添加/删除最后一个逗号;例如您可以简单地交换 2 行,而无需删除和添加新的逗号:

This is to conform with the semicolon rules, and also so that you can rearrange and add new lines without having to take care of adding / removing the final comma; e.g. you can simply swap the 2 lines without having to remove and to add a new comma:

s := []int {
    4, 5, 6,
    1, 2, 3,
}

在列出函数调用的参数时同样适用:

The same applies when listing arguments to a function call:

fmt.Println("first",
    "second",
    "third",       // Note it ends with a comma
)

这篇关于如何在 Golang 中打破一长行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆