英文:
Comparing value of nil pointer variable
问题
我知道Go在这方面与其他语言有些不同,但我想知道对于Go语言来说,自动检测空指针并在比较时返回不相等是否值得。而不是创建运行时错误。Go在许多方面都非常出色,我只是觉得它可能会这样做。以下是一个例子:
<pre><code>
type cuForm struct {sName string; iUseCount int64; baForm []byte}
var pugForm *cuForm
//***********************************************************
func loadForm (sWanted string) (cuForm, os.Error) {
//**********************************************************
if (sWanted == pugForm.sName) {
</code></pre>
在上面的例子中,如果pugForm是nil,就会发生运行时错误。显然,如果只有一个值是nil,它们不可能相等-至少在逻辑上是如此。可能有一些原因不应该这样做,但我认为历史不应该是一个原因。
英文:
I know that Go is little different to other languages in this respect, but I wondered if it would be worthwhile for the Go language to automatically test for nil pointers on comparisons and return unequal if only one of the values is nil (in a comparison) rather than creating a run-time error. Go is so great in so many areas I just thought it might do it. The case in point is as follows:
<pre><code>
type cuForm struct {sName string; iUseCount int64; baForm []byte}
var pugForm *cuForm
//***********************************************************
func loadForm (sWanted string) (cuForm, os.Error) {
//**********************************************************
if (sWanted == pugForm.sName) {
</code></pre>
In the above example, if pugForm is nil, a runtime error occurs. Obviously if only one of the values is nil, it cannot be equal - logically at least. There may well be a reason why this shouldn't be done, but I don't think history should be one reason.
答案1
得分: 1
错误信息是:“panic: runtime error: invalid memory address or nil pointer dereference.” 实际错误是空指针解引用。
当指针pugForm为空时,pugForm.sName未定义。你希望它是一个特殊的值,比如SQL中的null或浮点数中的NaN。现在你需要一组特殊的规则来处理所有操作,而不仅仅是相等性。
空指针解引用几乎总是错误的。如果发生这种情况,运行时应该报错。如果不是错误的,通过测试是否为空来避免问题。修复你的错误,不要试图假装它没有发生。
你对这个程序的输出有什么期望?
package main
import (
"fmt"
)
func main() {
var i int
var p *int
var b bool
b = i == *p
b = i <= *p
b = i >= *p
i += *p
i -= *p
i *= *p
i /= *p
i %= *p
i = *p << uint(i)
i = *p >> uint(i)
fmt.Println(i, p, b)
}
英文:
The error message is: "panic: runtime error: invalid memory address or nil pointer dereference." The actual error is a nil pointer dereference.
When the pointer pugForm is nil then pugForm.sName is undefined. You want it to be a special value like null in SQL or NaN in floating-point. Now you need a special set of rules for all operations, not just equality.
A nil pointer dereference is almost always wrong. The runtime should object if that happens. If it's not wrong, avoid the problem by testing for nil. Fix your error, don't try to pretend it didn't happen.
What output do you expect from this program?
package main
import (
"fmt"
)
func main() {
var i int
var p *int
var b bool
b = i == *p
b = i <= *p
b = i >= *p
i += *p
i -= *p
i *= *p
i /= *p
i %= *p
i = *p << uint(i)
i = *p >> uint(i)
fmt.Println(i, p, b)
}
答案2
得分: 0
并不是说你的问题不相关,我认为不将其作为默认行为实现是有充分理由的:这意味着在运行时需要进行更多的测试,而我不希望对我的程序的所有相等性测试都进行这样的测试。
但是确实有一个专用的表达式(例如类似于'==='的东西)可能会有用。
正如Nos所说,go-nuts邮件列表可能是一个更有建设性的讨论场所。
英文:
Not saying that your question isn't pertinent, I see a good reason to not implement this as the default comportment : that means more tests at runtime and I don't want that for all equality tests of my programs.
But it's true that a dedicated expression (for example something like '===') could (maybe) be useful.
As Nos said, the go-nuts mailing list would probably be a more constructive place for this discussion.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论