Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected <type> at the end of statement

Tags:

go

The Go code is below.

Error Message: syntax error: unexpected float64 at end of statement in line 9.

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

I don't know what is wrong. Who know that?

like image 672
spritecodej Avatar asked Jul 08 '16 15:07

spritecodej


2 Answers

Change line 9 to either of statements below:

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

or

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

See short variable declarations.

like image 66
Grzegorz Żur Avatar answered Sep 20 '22 12:09

Grzegorz Żur


Variable declaration in Go

static var var_name data_type = value

or

dynamic: var_name := value

In your case at line 9 you have missed both standard

like image 42
AlexanderAdade Avatar answered Sep 22 '22 12:09

AlexanderAdade