dongmin4990 2015-11-13 22:56
浏览 151
已采纳

语法错误:名称意外,应使用分号或换行符或}

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
}

展开全部

  • 写回答

2条回答 默认 最新

  • drzdu44226 2015-11-13 22:59
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部