语法错误:意外的名称,期望分号、换行符或}。

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

syntax error: unexpected name, expecting semicolon or newline or }

问题

作为前导,我最近才开始学习Go语言。这可能是我花了第三天的时间。我已经花了几个小时来解决这个错误,但是我无法找出问题出在哪里。

这是抛出的错误:
语法错误:意外的名称,期望分号或换行符或}

可能是一些简单的问题,但是由于我对Go语言的知识有限,我无法确定具体问题所在。任何帮助将不胜感激。

编辑
这是最终的工作函数,除了像接受的答案所说的重命名变量之外,我还必须使生成器返回一个返回int的函数。我还有一个关于斐波那契逻辑的错误。

func fibGenerator() func() uint {
  var (
    n uint = 0
    back1 uint = 1
    back2 uint = 0
  )
    
  _computeFib := func() uint {
    if n == 0 {
      n++
      return 0
    } else if n == 1 {
      n++
      return 1
    }
    fib := back1 + back2
    back2 = back1
    back1 = fib
    n++
    return fib
  }
  
  return _computeFib
}
英文:

Just as a precursor I've just barely started learning Go recently. This is probably my 3rd day spending some time on it. I've been working with this error for a couple hours now, and I can't figure out what is wrong.

package main

import "fmt"

func main () {
  nextFib := fibGenerator();
  fmt.Println(nextFib());
  fmt.Println(nextFib());
  fmt.Println(nextFib());
  fmt.Println(nextFib());
  fmt.Println(nextFib());
}

func fibGenerator () uint {
  var (
    n uint = 0
    back1 uint = 1
    back2 uint = 0
  )
    
  _computeFib := func () uint {
    if n == 0 {
      n++
      return 0
    } else if n == 1 {
      n++
      return 1
    }
    fib := 1back + 2back // throws compile time error on this line
    2back = 1back
    1back = n
    n++
    return fib
  }
  
  return _computeFib
}

This is the error it throws:
syntax error: unexpected name, expecting semicolon or newline or }

It's probably something simple, but with my limited knowledge in Go I can't put my finger on it. Any help would be appreciated.

EDIT:
This is the final working function besides renaming my variables like the accepted answer says I also had to make the generator return a function that returns an int. I also had an error w/ Fibonacci logic.

func fibGenerator () func() uint {
  var (
    n uint = 0
    back1 uint = 1
    back2 uint = 0
  )
    
  _computeFib := func () uint {
    if n == 0 {
      n++
      return 0
    } else if n == 1 {
      n++
      return 1
    }
    fib := back1 + back2
    back2 = back1
    back1 = fib
    n++
    return fib
  }
  
  return _computeFib
}

答案1

得分: 2

重构问题搁置不谈,记住在Go语言中,变量必须以字母开头,而不是数字。back1back2是有效的Go变量,但1back2back不是。请参考https://golang.org/ref/spec#Identifiers。

英文:

Refactoring issues aside, keep in mind that variables in go must begin with a letter, not a number. back1 and back2 are valid go variables, but 1back and 2back are not. See https://golang.org/ref/spec#Identifiers.

答案2

得分: 1

你正在尝试访问名为1back2back的变量,但实际上你的变量名应该是back1back2

英文:

You are trying to access variables called 1back and 2back but your variables are actually called back1 and back2

huangapple
  • 本文由 发表于 2015年11月14日 14:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/33705973.html
匿名

发表评论

匿名网友

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

确定