这个简单脚本有什么问题?

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

What's wrong with this simple script?

问题

我正在翻译以下内容:

我正在学习函数,为教科书编写了一个简单的脚本,但出现了两个错误。

package main
import "fmt"

func zero(x int) {
    x = 0
    return x
}
func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}

> 返回值过多 (返回值为字符串 x)

为什么是"过多"?只有一个返回值啊!

> zero(x) 作为值使用 (字符串 x = zero(x))

我不明白他对我说了什么。

英文:

I study functions, wrote a simple script for the textbook, and there were 2 errors.

package main
import "fmt"

func zero(x int) {
    x = 0
	return x
}
func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}

> too many arguments to return (string return x)

How is "too many"? It's only one!

> zero(x) used as value (string x = zero(x))

I don't understand what he says to me.

答案1

得分: 1

int in func

package main
import "fmt"

func zero(x int) int {
    x = 0
    return x
}
func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}

在这段代码中,定义了一个名为"zero"的函数,它接受一个整数参数x,并将x的值设为0,然后返回x的值。在"main"函数中,定义了一个变量x并赋值为5,然后调用"zero"函数,并将返回值赋给x,最后打印输出x的值。

英文:

int in func

package main
import "fmt"

func zero(x int) int {
    x = 0
    return x
}
func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}

答案2

得分: 0

package main

import "fmt"

func zero(x int) int {
    x = 0
    return x
}

func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}
package main

import "fmt"

func zero(x int) int {
    x = 0
    return x
}

func main() {
    x := 5
    x = zero(x)
    fmt.Println(x)
}

这是一个简单的Go语言程序。它定义了一个名为zero的函数,该函数接受一个整数参数x,将其赋值为0,并返回该值。在main函数中,我们声明了一个变量x并初始化为5,然后调用zero函数将x赋值为0,并打印输出结果。运行该程序将输出0。

英文:
package main

import "fmt"

func zero(x int) int {
	x = 0
	return x
}

func main() {
	x := 5
	x = zero(x)
	fmt.Println(x)
} 

答案3

得分: 0

我相信这更接近原始想法...

package main

import "fmt"

func zero(x *int) {
    *x = 0
    return
}

func main() {
    x := 5
    zero(&x)
    fmt.Println(x)
}
英文:

I believe this is closer to the original idea...

package main

import "fmt"

func zero(x *int) {
	*x = 0
	return
}

func main() {
	x := 5
	zero(&x)
	fmt.Println(x)
}

答案4

得分: 0

"too many" 意味着你的函数返回的值比函数签名指定的多。

在你的情况下,你的函数签名 func zero(x *int) 表示这个函数不返回任何参数,但是在函数体内,你返回了一个值:return x。所以 1 比期望的 0 多了 1 个。

然后 "zero(x) used as value" 是告诉你正在调用一个不返回任何值的函数,并且你试图将不存在的返回值赋给一个变量:x = zero(x)

这就是编译器告诉你关于使用 zero(x) 作为值的原因。

英文:

too many means that your function is returning more values that the function signature specifies.

In your case, your function signature func zero(x *int), says that this function doesn't returns ANY params, and inside the function body, you're returning ONE value: return x. So 1 is too many for 0 expected. Exactly 1 more.

Then zero(x) used as value is telling you that you're calling a function that doesn't return ANY value, and you're trying to assign the non-existent return value to a variable: x = zero(x).

That's why the compiler tells you about using zero(x) as a value

huangapple
  • 本文由 发表于 2017年6月21日 18:40:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/44673830.html
匿名

发表评论

匿名网友

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

确定