语句末尾出现意外的<type>

spr*_*dej 7 go

Go代码如下.

错误消息:语法错误:第9行语句末尾的意外float64.

package main

import (
    "fmt"
    "math"
)

func pow(x, n, lim float64) float64 {
    v float64 = math.Pow(x,n) // line 9
    if v<lim {
        return v
    } else {
        fmt.Printf("%g >= %g\n", v, lim)
    }   
    return lim 
}

func main() {
    fmt.Println(
        pow(3, 2, 10),
        pow(3, 2, 20),
    )   
}
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题.谁知道呢?

Grz*_*Żur 9

将第9行更改为以下任一语句:

v := math.Pow(x,n) // implicit type declaration and assignment
Run Code Online (Sandbox Code Playgroud)

要么

var v float64 = math.Pow(x,n) // explicit type declaration and assignment
Run Code Online (Sandbox Code Playgroud)

请参见简短变量声明.