Thanks Ian and Dominik for the tips. I have checked the issue 72860 and 
found randall77 posted a comment that
I couldn't confirm from golang spec(go.dev/ref/spec):

//https://github.com/golang/go/issues/72860#issuecomment-2734549770
```

Where f returns a pointer and an error. f expects its callers to only use 
the ptr result if the error is nil. And in particular, f returns a nil 
pointer whenever it returns a non-nil error.

This code is incorrect, in that it dereferences the ptr result before 
checking the error. The Go spec says that it should nil pointer panic if f 
returns 
a nil pointer and non-nil error. 
```

I did a search in whole spec and did't find any descriptions about it.
>From my understanding, `v, err := f()` is like `v1, v2 := f()` from 
compiler's view, 
checking error is user code's business logic and couldn't be assumed by 
compiler.
If it's defined in spec, what about variants like `a, b, err := f()` or `a, 
b, err1, err2 := f()`?

Ge
在2025年7月6日星期日 UTC+8 07:52:56<Dominik Honnef (dominikh)> 写道:

> Memory beats git bisect, but this was indeed fixed by 
> a1ddbdd3ef8b739aab53f20d6ed0a61c3474cf12.
>
> On Saturday, July 5, 2025 at 6:36:57 PM UTC+2 Ian Lance Taylor wrote:
>
>> On Sat, Jul 5, 2025 at 8:24 AM Ge <everg...@gmail.com> wrote: 
>> > 
>> > As follows showing, when `-N` is enabled with compiler, it gives the 
>> expected output. 
>> > ``` 
>> > ➜ grd more nf2.go 
>> > package main 
>> > 
>> > import "os" 
>> > 
>> > func main() { 
>> > f, err := os.Open("nonExistFile") 
>> > fname := f.Name() 
>> > if err != nil { 
>> > println("failed to open file, Error:", err.Error()) 
>> > return 
>> > } 
>> > 
>> > println("filename:", fname) 
>> > } 
>> > ➜ grd go run nf2.go 
>> > failed to open file, Error: open nonExistFile: no such file or 
>> directory 
>> > 
>> > ➜ grd go run -gcflags="-N" nf2.go 
>> > panic: runtime error: invalid memory address or nil pointer dereference 
>> > [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4782e3] 
>> > 
>> > goroutine 1 [running]: 
>> > os.(*File).Name(...) 
>> > /usr/local/go/src/os/file.go:62 
>> > main.main() 
>> > /root/grd/nf2.go:7 +0x123 
>> > exit status 2 
>> > 
>> > ➜ grd go version 
>> > go version go1.24.4 linux/amd64 
>> > ``` 
>>
>> This may be https://go.dev/issue/72860. In any case, it works 
>> correctly (that is, it panics) on HEAD and the upcoming Go 1.25 
>> release. 
>>
>> Ian 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/4dc50973-96ca-4a6f-aabc-ac0352375f37n%40googlegroups.com.

Reply via email to