non-declaration statement outside function body error in GO

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

non-declaration statement outside function body error in GO

问题

我是一个新学习者,正在学习Go语言,这些问题让我很困惑。我无法解决它们,你们能帮我一下吗?

func Solution(A []int, B []int, K int) int {
    .......
    res = MaxInt32 
    low = 0
    high = Min(900, largestId) //largestId在这里有限制
    mid = 0

    for low <= high {
        mid = (low + high) / 2
        if isAvailable(K, mid) {
            res = Min(res, mid)
            high = mid - 1
        } else {
            low = mid + 1
        }
    }
    return res
}

错误显示为:

workspace/src/solution/solution.go:55: 语法错误: 意外的 =,期望 }
workspace/src/solution/solution.go:64: 非声明语句在函数体外
workspace/src/solution/solution.go:65: 语法错误: 意外的 }

我不明白为什么会出现这些问题?

英文:

I am a new learner with Go, and these problems confused me a lot. I cannot solve them, could you guys give me a hand?

func Solution(A []int, B[]int, K int) int{
.......
res = MaxInt32 
low = 0
high = Min(900, largestId) //largestId is limited here
mid = 0

while(low &lt;= high){
	mid = {low + high} / 2         55
	if(isAvailable(K, mid)){
		res := Min(res, mid)
		high :=mid - 1
	} else{
		low := mid + 1
	}
	
}
return res                         64
}                                  65

The errors show:

workspace/src/solution/solution.go:55: syntax error: unexpected =, expecting }
workspace/src/solution/solution.go:64: non-declaration statement outside function body
workspace/src/solution/solution.go:65: syntax error: unexpected }

I don't understand why these problems come?

答案1

得分: 2

在Go语言中没有while循环,只有for循环。如果我这样写:

package main

func main() {
    var n int
    while (n < 10) {
        n++
    }
    return
}

我会得到类似于你的错误信息:

untitled 3:6: syntax error: unexpected ++, expecting }
untitled 3:8: non-declaration statement outside function body
untitled 3:9: syntax error: unexpected }

如果我将 while n < 10(去掉括号)这样写,我会得到更精确的错误信息,即在第5行(while)上出现了一个“意外的名称”错误。我认为由于括号的使用,编译器将(非保留字)while 视为类型(函数调用或类型转换),但在意识到它不存在之前,还有其他错误要报告。因此,也许对你来说这是一个令人困惑的消息。

除非你的代码中还有其他错误,将 while 重命名为 for 应该可以解决问题。并且去掉括号。

英文:

There's no while loop in Go. Only for. If I do this:

package main

func main() {
	var n int
	while (n &lt; 10) {
		n++
	}
	return
}

I get the following error (similar to yours):

untitled 3:6: syntax error: unexpected ++, expecting }
untitled 3:8: non-declaration statement outside function body
untitled 3:9: syntax error: unexpected }

If I do while n &lt; 10 (no parentheses) I get more precise message, i.e. an unexpected name error on line 5 (while). I believe due to a bracket usage compiler treats (non-reserved word) while as a type (function call or type conversion), but before realising it's non-existent there are other errors to report. Hence, maybe, a confusing message for you.

Unless you have other errors in your code, renaming while to for should work. And drop the brackets.

答案2

得分: 0

例如,

package main

import (
	"math"
)

func Min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func isAvailable(k, mid int) bool {
	// ...
	return true
}

func Solution(A []int, B []int, K int) int {
	largestId := 0
	// ...
	res := math.MaxInt32
	low := 0
	high := Min(900, largestId)

	for low <= high {
		mid := (low + high) / 2
		if isAvailable(K, mid) {
			res = Min(res, mid)
			high = mid - 1
		} else {
			low = mid + 1
		}

	}
	return res
}

func main() {}

你需要学习基本的Go语法。参考Go之旅

英文:

For example,

package main

import (
	&quot;math&quot;
)

func Min(a, b int) int {
	if a &gt; b {
		return b
	}
	return a
}

func isAvailable(k, mid int) bool {
	// ...
	return true
}

func Solution(A []int, B []int, K int) int {
	largestId := 0
	// ...
	res := math.MaxInt32
	low := 0
	high := Min(900, largestId)

	for low &lt;= high {
		mid := (low + high) / 2
		if isAvailable(K, mid) {
			res = Min(res, mid)
			high = mid - 1
		} else {
			low = mid + 1
		}

	}
	return res
}

func main() {}

You need to learn basic Go syntax. Take the Go Tour.

huangapple
  • 本文由 发表于 2015年11月17日 21:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/33758834.html
匿名

发表评论

匿名网友

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

确定