我正在尝试进入 Go,但无法打印东西。在我的主文件中,我有:
package main
import (
"fmt"
"go-backprop/expression"
)
func main() {
fmt.Println("test")
var e expression.Constant = expression.Constant{Value: 3.0}
fmt.Println("test2")
fmt.Println(e.Value)
}
随着Constant being的实施:
package expression
type Constant struct {
Value float32
}
func (c *Constant) GetCache() float32 {
return c.Value
}
func (c *Constant) Evaluate() float32 {
return c.Value
}
这拒绝打印
test1
或 test2
,但是当我注释掉试图调用 e.Value 时,它会打印两者。当我用 e.GetCache() 或 e.Evaluate() 替换 e.Value 时,它也拒绝打印。这是为什么?
我试过在代码应该运行后为 main 的执行添加延迟,我试过为该值创建一个 getter,我试过使用接口,我什至试过将我的 Constant 代码移到我的主目录中.所有这些都会导致尝试调用 e 的任何属性,从而使所有输出到标准输出无效。