gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:05:55 27 4
gpt4 key购买 nike

编译以下代码时...

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
}

...出现以下错误。

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 行的错误!谁能解释一下?

最佳答案

隔离 in a playgroud ,它编译得很好,并且有预期的输出:

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

您需要尝试使用包含该代码的camera.go,或者检查camera.go 文件如何适合您的current workspace .

关于Go 方法参数和 float64 数组给出意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25519638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com