How do I update a value of a variable in an if statement in Go?

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

How do I update a value of a variable in an if statement in Go?

问题

我正在尝试学习Go语言,并创建了一个函数。在这个函数中,我声明了一个名为game_ratio的变量,并将其设置为0.0。然后我使用一个if语句来尝试更新game_ratio的值。但是当我尝试编译时,我收到以下错误信息:
'game_ratio declared and not used'

以下是我的函数代码:

func gameRatio(score1 int, score2 int, max_score float64) float64 {
    var game_ratio float64 = 0.0
    var scaled_score_1 = scaleScore(score1, max_score)
    var scaled_score_2 = scaleScore(score2, max_score)
    fmt.Printf("Scaled score for %v is %v\n", score1, scaled_score_1)
    fmt.Printf("Scaled score for %v is %v\n", score2, scaled_score_2)
    if score1 > score2 {
        game_ratio := (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5
    }
    return game_ratio
}

以下是调用该函数的代码:

func main() {
    flag.Parse()
    s1 := flag.Arg(0)
    s2 := flag.Arg(1)
    i1, err := strconv.Atoi(s1)
    i2, err := strconv.Atoi(s2)
    if err != nil {
        fmt.Println(err)
        os.Exit(2)
    }
    fmt.Println("Game ratio is", gameRatio(i1, i2, 6))
}

我还编写了另一个名为scaleScore的函数。如果我移除if语句,代码就可以正常工作。

要运行我的应用程序,我输入'rankings 28 24'。

英文:

I'm trying to learn Go and I've created a function where I declare a variable game_ratio and set it to 0.0. I then have an if statement where I try and update the value of game_ratio. When I try and compile, I get the following error message:
'game_ratio declared and not used'

Here's my function:

func gameRatio(score1 int, score2 int, max_score float64) float64 {
    var game_ratio float64 = 0.0
    var scaled_score_1 = scaleScore(score1, max_score)
    var scaled_score_2 = scaleScore(score2, max_score)
    fmt.Printf("Scaled score for %v is %v\n", score1, scaled_score_1)
    fmt.Printf("Scaled score for %v is %v\n", score2, scaled_score_2)
    if score1 > score2 {
        game_ratio := (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5
    }
    return game_ratio
}

Here's the code to call it:

func main() {
    flag.Parse()
    s1 := flag.Arg(0)
    s2 := flag.Arg(1)
    i1, err := strconv.Atoi(s1)
    i2, err := strconv.Atoi(s2)
    if err != nil {
        fmt.Println(err)
        os.Exit(2)
    }
    fmt.Println("Game ratio is", gameRatio(i1, i2, 6))
}

ScaleScore is another function I have written. If I remove the if statement, the code works.

To run my app, I type 'rankings 28 24'

答案1

得分: 4

短变量声明重新声明了game_ratio

使用赋值语句进行修改:

game_ratio = (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5

短变量声明是一种简写形式,它使用以下语法:

ShortVarDecl = IdentifierList ":=" ExpressionList

它相当于一个带有初始化表达式但没有类型的常规变量声明:

"var" IdentifierList = ExpressionList

与常规变量声明不同,短变量声明可以重新声明变量,前提是它们在同一代码块中以相同的类型进行了原始声明,并且至少有一个非空白变量是新的。因此,重新声明只能出现在多变量的短声明中。重新声明不会引入新变量;它只是给原始变量赋予新值。

英文:

The short variable declaration is redeclaring game_ratio.

game_ratio := (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5

Use an assignment. Write:

game_ratio = (scaled_score_1+1.0)/(scaled_score_1+scaled_score_2+2.0) + 1.0*0.5

> The Go Programming Language Specification
>
> Short variable declarations
>
> A short variable declaration uses the syntax:
>
> ShortVarDecl = IdentifierList ":=" ExpressionList .
>
> It is shorthand for a regular variable declaration with initializer
> expressions but no types:
>
> "var" IdentifierList = ExpressionList .
>
> Unlike regular variable declarations, a short variable declaration may
> redeclare variables provided they were originally declared earlier in
> the same block with the same type, and at least one of the non-blank
> variables is new. As a consequence, redeclaration can only appear in a
> multi-variable short declaration. Redeclaration does not introduce a
> new variable; it just assigns a new value to the original.

huangapple
  • 本文由 发表于 2014年7月27日 01:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/24973665.html
匿名

发表评论

匿名网友

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

确定