【问题标题】:Unexpected <type> at the end of statement语句末尾出现意外的 <type>
【发布时间】:2020-07-04 17:43:58
【问题描述】:

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),
    )   
}

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

【问题讨论】:

    标签: go


    【解决方案1】:

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

    v := math.Pow(x,n) // implicit type declaration and assignment
    

    或

    var v float64 = math.Pow(x,n) // explicit type declaration and assignment
    

    见short variable declarations。

    【讨论】:

      【解决方案2】:

      Go 中的变量声明

      静态 var var_name data_type = 值

      或

      动态: var_name := 值

      在第 9 行的情况下,您错过了两个标准

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        相关资源
        最近更新 更多