当代码中声明了函数但未调用时,Go 会发生 panic。

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

Go panics on when having functions declared in code but not called

问题

上下文中的代码段是关于在尝试设置gorm时转换标准的revel/gorp示例的一部分。

然而,即使定义了Begin()、Commit()、Rollback()方法,也足以导致Go进入panic状态。

将方法定义注释掉可以阻止panic。我似乎无法理解为什么不调用这些方法(注意revel.InterceptMethod调用已被注释掉)会导致panic发生:/

英文:

Context:
https://github.com/fusspawn/tserver/blob/master/app/controllers/gorp.go

As part of trying to get gorm setup I tried to convert the standard revel/gorp examples.

However. even having the Begin(),Commit(),Rollback() methods defined is enough to cause go to panic.

   TRACE 2015/09/06 17:37:47 harness.go:126: Rebuild                                                                                                  
   INFO  2015/09/06 17:37:47 build.go:172: Cleaning dir tmp                                                                                           
   INFO  2015/09/06 17:37:47 build.go:172: Cleaning dir routes                                                                                        
   INFO  2015/09/06 17:37:47 build.go:172: Cleaning dir tmp                                                                                           
   INFO  2015/09/06 17:37:47 build.go:172: Cleaning dir routes                                                                                        
   TRACE 2015/09/06 17:37:47 build.go:151: Exec: [/usr/bin/git --git-   dir=/home/fusspawn/go/src/github.com/fusspawn/tserver/.git describe --always --   dirty]                                                                                                                                               
   TRACE 2015/09/06 17:37:47 build.go:94: Exec: [/usr/bin/go build -ldflags    -X github.com/fusspawn/tserver/app.APP_VERSION "git-2588ef1" -tags  -o /ho
  me/fusspawn/go/bin/revel.d/github.com/fusspawn/tserver/tserver   github.com/fusspawn/tserver/app/tmp]                                                
   TRACE 2015/09/06 17:39:14 app.go:56: Exec app:   /home/fusspawn/go/bin/revel.d/github.com/fusspawn/tserver/tserver    [/home/fusspawn/go/bin/revel.d/git
   hub.com/fusspawn/tserver/tserver -port=46276 -  importPath=github.com/fusspawn/tserver -runMode=dev]                                                 
   INFO  2015/09/06 17:39:14 revel.go:329: Loaded module static                                                                                       
   INFO  2015/09/06 17:39:14 revel.go:329: Loaded module testrunner                                                                                   
   INFO  2015/09/06 17:39:14 revel.go:206: Initialized Revel v0.12.0 (2015-03-25) for >= go1.3                                                        
   INFO  2015/09/06 17:39:14 main.go:30: Running revel server                                                                                         
   TRACE 2015/09/06 17:39:14 controller.go:402: Registered controller: App                                                                            
   panic: reflect: call of reflect.Value.Type on zero Value                                                                                           
                                                                                                                                               
   goroutine 1 [running]:                                                                                                                             
   runtime.panic(0x88cec0, 0xc21011b7a0)                                                                                                              
    /usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6                                                                                              
   reflect.Value.Type(0x0, 0x0, 0x0, 0x0, 0x0)                                                                                                        
    /usr/lib/go/src/pkg/reflect/value.go:1698 +0x79                                                                                            
    github.com/revel/revel.findControllers(0x7f365698bea8, 0x9186e0, 0x0, 0x0, 0x0)                                                                    
    /home/fusspawn/go/src/github.com/revel/revel/controller.go:321 +0x25a                                                                      
    github.com/revel/revel.RegisterController(0x9172a0, 0x0, 0xc2100c4bf0,    0x2, 0x2)                                                                   
     /home/fusspawn/go/src/github.com/revel/revel/controller.go:400 +0x1f3                                                                      
    main.main()                                                                                                                                        
     /home/fusspawn/go/src/github.com/fusspawn/tserver/app/tmp/main.go:150  +0x141b                                                              
     TRACE 2015/09/06 17:39:14 controller.go:402: Registered controller:     GormController                                                                 
     TRACE 2015/09/06 17:39:14 controller.go:402: Registered controller: Static                                                                         
     TRACE 2015/09/06 17:39:14 controller.go:402: Registered controller:  TestRunner             

Commenting out the method definitions will stop the panic. I cant seem to understand how not calling these methods (note the revel.InterceptMethod calls are commented out) can make the panic happen :/

答案1

得分: 2

根据 Revel 文档,系统在调试模式下通过扫描源代码目录中的内容来查找控制器。你看到的错误是由于 Revel 在进行控制器扫描初始化期间进行动态反射时出现问题。现在的问题是:为什么它在扫描控制器时出现问题?

你的代码似乎试图在 EventStream 中扩展一个控制器,而 Revel 调试控制器发现代码将尝试将其读取为一个控制器。然而,目前的代码违反了框架的预期,它嵌入了一个 *GormController 而不是 GormController,请参考控制器文档的最后部分。

我认为控制器发现逻辑只是因为这个问题而出错了。修复 EventStream 结构定义。

如果这样修复了你的问题,你可能应该向 Revel 的开发人员发送一个错误报告,因为你收到的错误消息非常糟糕且不具有局部性,因为它在失败时没有提到它试图注册为控制器的名称。

英文:

According to the Revel docs, the system looks for Controllers in debug mode by scanning your source directory for anything that does an anonymous embedding of a *Revel.Controller. The error your're seeing is due to Revel doing dynamic reflection and screwing up during this controller-scanning initialization. So now the question is: why is it having trouble scanning for controllers?

Your code appears to try to extend a controller in EventStream, which the Revel debug controller-discovery code will try to read as a Controller. However, the code as it stands is currently violating expectations of the framework by embedding a *GormController instead of a GormController; see the last part of the Controllers documentation.

I think the controller-discovery logic is simply choking because of this. Fix the EventStream structure definition.

If this does correct the issue for you, you should probably send a bug report to the Revel folks, because the error message you're getting back is pretty bad and non-local, because it doesn't mention the name of the thing it's trying to register as a controller when it fails.

huangapple
  • 本文由 发表于 2015年9月7日 01:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/32426216.html
匿名

发表评论

匿名网友

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

确定