英文:
Declare mutiple variables on the same line with types in Go
问题
我有以下的代码片段:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
fmt.Println("请输入你的名字")
var name string
var err error
name, err = reader.ReadString('\n') //这一行
if err == nil {
fmt.Println("你好," + name)
}
}
我的问题是,如果我不想使用:=
语法(就像我在main()
的第一行所做的那样),我该如何使用类型重写ReadString()
的调用?
我尝试了以下几种方式,但都出现了相应的错误:
var name string, err error = reader.ReadString('\n')
->语法错误:语句末尾的逗号意外
var name, err string, error = reader.ReadString('\n')
->语法错误:语句末尾的逗号意外
- 参考了https://stackoverflow.com/questions/45086082/multiple-variables-of-different-types-in-one-line-in-go-without-short-variable 中的提示,我还尝试了
var (name string, err error) = reader.ReadString('\n')
,但仍然出现了相同的错误。
对于上面链接中的问题,被标记为答案的回答只是建议为两种不同的变量类型使用两行。但是对于像ReadString()
这样的函数的返回值,这样做会怎么样呢?
英文:
I have the below code snippet:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
fmt.Println("Enter your name")
name, err := reader.ReadString('\n') //THIS LINE
if err == nil {
fmt.Println("Hello " + name)
}
}
My question is, if I want to NOT use the :=
syntax (like I have at the first line of main()
), how would I rewrite the ReadString()
invocation with types?
I tried the following, with the corresponding errors:
var name string, err error = reader.ReadString('\n')
->syntax error: unexpected comma at end of statement
var name, err string, error = reader.ReadString('\n')
->syntax error: unexpected comma at end of statement
- Taking a hint from https://stackoverflow.com/questions/45086082/multiple-variables-of-different-types-in-one-line-in-go-without-short-variable I also tried
var (name string, err error) = reader.ReadString('\n')
which also gives the same error.
For the above linked question, the marked answer simply suggests using two lines for two different variable types. But how would that work for the return values of a function like ReadString()
?
答案1
得分: 3
首先,
name, err := reader.ReadString('\n')
是完全正确的。如果你不知道ReadString()
的返回值类型,大多数IDE都会显示给你。
正如链接的答案所详细说明的那样,变量声明最多只能有一个可选类型,因此无法指定两个类型。
如果你觉得类型不可见很困扰你,那意味着对你来说可读性更重要。如果是这样,就不要坚持那种“一行胜过一切”的理念。
如果你希望在源代码中看到类型,可以先声明类型,然后使用赋值:
var (
name string
err error
)
name, err = reader.ReadString('\n')
如果你仍然需要一行代码(只是为了好玩),那就需要一个辅助函数。辅助函数的名称可以“说明”预期的类型:
func stringAndError(s string, err error) (string, error) {
return s, err
}
然后你可以使用变量声明或短变量声明:
var name, err = stringAndError(reader.ReadString('\n'))
// 或者
name, err := stringAndError(reader.ReadString('\n'))
英文:
First of all,
name, err := reader.ReadString('\n')`
is perfectly fine. Most IDE's will display you the types of the return values of ReadString()
if you would not know them.
As the linked answer details, a variable declaration can have one optional type at most, so specifying 2 types is not possible.
If it bothers you that the types are not visible, that means readability is more important to you. If it is, break with that "one-liners-for-the-win" philosophy.
If you want the types to be visible in the source code, declare the types prior, and use assignment:
var (
name string
err error
)
name, err = reader.ReadString('\n')
If you still need a one liner (just for fun), it requires a helper function. The name of the helper function can "state" the expected types:
func stringAndError(s string, err error) (string, error) {
return s, err
}
Then you can use either a variable declaration or a short variable declaration:
var name, err = stringAndError(reader.ReadString('\n'))
// OR
name, err := stringAndError(reader.ReadString('\n'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论