英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论