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.
Thanks Ian and Dominik for the tips. I have checked the issue 72860 and found randall77 posted a comment thatI couldn't confirm from golang spec(go.dev/ref/spec):```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()`?
Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.
Anyway: the point here is that if a function returns (ptr, err) then in the case err = nil it will conventionally guarantee that ptr is valid, so the caller doesn't need to check ptr != nil before dereferencing it. But if err != nil, no such guarantee is implied, and it's highly unlikely that you could safely deference ptr. That convention is not in the spec, but dereferencing a nil pointer causing a panic is:On Jul 7, 2025, at 10:43 AM, 'Keith Randall' via golang-nuts <golan...@googlegroups.com> wrote:
See the second paragraph in the compiler section of the go1.25 release notes: https://tip.golang.org/doc/go1.25#compiler
--
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...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/61d20eb5-8102-429e-a29c-17a3da0314f3n%40googlegroups.com.
This change seems wrong just based on the io.Reader returns of valid bytes and an error. An error doesn’t always mean the other return values are invalid - it is decided by the api method.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/7D77D6E6-277A-4014-8F4D-C034B3973526%40ix.netcom.com.
This change seems wrong just based on the io.Reader returns of valid bytes and an error.
An error doesn’t always mean the other return values are invalid - it is decided by the api method.
On Jul 7, 2025, at 10:53 AM, Ian Lance Taylor <ia...@golang.org> wrote:
On Jul 7, 2025, at 11:25 AM, Robert Engels <ren...@ix.netcom.com> wrote:
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/92C78124-EAF8-4E6C-B239-D888061F634A%40ix.netcom.com.