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

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

问题描述

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

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

我试图将它分解为

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

但是我得到了

 语法错误:意外\ 

我也试过打破输入并在末尾输入分号:

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

但我再次得到:

 语法错误:意外。 

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

解决方案

首先是一些背景。 Go的形式语法使用分号;作为许多作品的终结符,但Go程序可能会忽略其中的大部分(并且它们应该有一个更清晰易读的来源; gofmt 也会删除不必要的分号)。



该规范列出了确切的规则。 规格:分号:


正式语法使用分号;作为一些作品的终结者。 Go程序可能会使用以下两条规则忽略大部分分号:


  1. 当输入分解为令牌时,分号是如果该标记为


  2. 为了让复杂的语句占据一行,在关闭}或}之前可以省略分号。



因此,您可以看到是否在括号之后插入换行符),分号; 将被自动插入,因此下一行不会被视为前一行的延续。这就是你的情况,所以下一行以 .Scan(&ReadUser.ID,... >开头,会给你一个编译时错误,因为这个站在本身(没有前一行)是编译时错误:语法错误:意外。



因此,您可能会在任何时候违反上述要点 1。以下列出的规则。



通常,您可以在逗号之后打开行,在打开括号之后,例如 [ {,以及点之后)。 可能引用了某个值的字段或方法,也可以在二元运算符(那些需要2个操作数的运算符)之后中断行,例如:

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

这里值得注意的一点是,如果你有一个结构体或切片或映射文字列出初始值你想在列出最后一个值后断行,你必须输入一个强制逗号,尽管这是最后一个值,并且不会有更多的值,例如:

  s:= [] int {
1,2,3,
4,5,6 ,//注意以逗号结尾
}

这符合分号规则,还可以重新排列和添加新行,而无需考虑添加/删除最终的逗号;例如你可以简单地交换2行而不必删除并添加一个新的逗号:

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




$ b

  fmt.Println(first,
second,
third,//注意以逗号结尾


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)

I tried to break it to

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

But I get

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

But the I again get:

syntax error: unexpected .

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

解决方案

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:

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

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

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 .

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

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
}

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天全站免登陆