Go方法参数和float64数组出现意外错误

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

Go method parameter and float64 array giving unexpected errors

问题

当编译以下代码时...

package camera

type camera struct {
    position [3]float64
    viewWidth int
    viewHeight int
}

func (c camera) SwitchToCartesianThreeSpace(x, y int) [3]float64 { // LINE 9
    var x3 float64 = 0 // 视图设置为原点
    var y3 float64 = float64(x) - (float64(c.viewWidth) / 2)
    var z3 float64 = float64(-y) + (float64(c.viewHeight) / 2)
    result := [3]float64{x3, y3, z3}                               // LINE 13
    return result
}

...会出现以下错误。

camera/camera.go:9: undefined: x
camera/camera.go:9: undefined: y
camera/camera.go:11: undefined: x
camera/camera.go:12: undefined: y
camera/camera.go:13: cannot use x3 (type float64) as type [3]float64 in array element
camera/camera.go:13: cannot use y3 (type float64) as type [3]float64 in array element
camera/camera.go:13: cannot use z3 (type float64) as type [3]float64 in array element

到目前为止,我已经写了相当多的Go代码,但我不明白为什么会出现第9行的错误或第13行的错误!有人能解释一下吗?

英文:

When compiling the following code...

package camera

type camera struct {
    position [3]float64
    viewWidth int
    viewHeight int
}

func (c camera) SwitchToCartesianThreeSpace(x, y int) [3]float64 { // LINE 9
    var x3 float64 = 0 // view is set to the origin
    var y3 float64 = float64(x) - (float64(c.viewWidth) / 2)
    var z3 float64 = float64(-y) + (float64(c.viewHeight) / 2)
    result := [3]float64{x3, y3, z3}                               // LINE 13
    return result
}

...the following errors occur.

camera/camera.go:9: undefined: x
camera/camera.go:9: undefined: y
camera/camera.go:11: undefined: x
camera/camera.go:12: undefined: y
camera/camera.go:13: cannot use x3 (type float64) as type [3]float64 in array element
camera/camera.go:13: cannot use y3 (type float64) as type [3]float64 in array element
camera/camera.go:13: cannot use z3 (type float64) as type [3]float64 in array element

I've written a fair amount of Go code so far and do not understand why the errors on line 9 or the errors on line 13 are occurring! Anyone able to explain?

答案1

得分: 2

在一个游乐场中进行隔离,它可以正常编译,并且有预期的输出:

c := camera{}
res := c.SwitchToCartesianThreeSpace(2, 3)
// res is [0 2 -3]

你需要尝试使用一个只包含这段代码的 camera.go 文件,或者检查一下 camera.go 文件在你当前的工作空间中的位置。

英文:

Isolating in a playgroud, it compiles just fine, and has the expected output:

c := camera{}
res := c.SwitchToCartesianThreeSpace(2, 3)
// res is [0 2 -3]

You need to try with a camera.go including only that code, or check how the camera.go file fits in your current workspace.

huangapple
  • 本文由 发表于 2014年8月27日 13:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/25519638.html
匿名

发表评论

匿名网友

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

确定