dongmin4990 2015-11-14 06:56
浏览 149
已采纳

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

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-14 06:59
    关注

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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度