Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect's FieldByName does not really return a zero value if field does not exist in a struct #43688

Closed
mitar opened this issue Jan 14, 2021 · 5 comments

Comments

@mitar
Copy link
Contributor

mitar commented Jan 14, 2021

What version of Go are you using (go version)?

go version go1.14.4 linux/amd64

Does this issue reproduce with the latest release?

Tested on go 1.15.6: https://play.golang.org/p/w-_XNjx9m34

What operating system and processor architecture are you using (go env)?

Reproducible on Go Playground.

What did you do?

I tried to get a field which does not exist of a struct using reflect.

What did you expect to see?

FieldByName documentation says:

FieldByName returns the struct field with the given name. It returns the zero Value if no field was found. It panics if v's Kind is not struct.

So I would expect that I can call IsZero on the returned value.

What did you see instead?

My program panics. Reproduction:

package main

import (
	"fmt"
	"reflect"
	"runtime"
)

type Foo struct {
	Bar string
}

func main() {
	fmt.Println(runtime.Version())
	foo := Foo{}
	v := reflect.ValueOf(foo)
	f := reflect.Indirect(v).FieldByName("Else")
	fmt.Printf("%t\n", f.IsZero())
}
go1.15.6
panic: reflect: call of reflect.Value.IsZero on zero Value

goroutine 1 [running]:
reflect.Value.IsZero(0x0, 0x0, 0x0, 0x4bee1b)
	/usr/local/go-faketime/src/reflect/value.go:1134 +0x488
main.main()
	/tmp/sandbox080632842/prog.go:18 +0x174

Instead, I have to use IsValid:

go1.15.6
false
@seankhliao
Copy link
Member

it returns a zero value reflect.Value struct (representing nothing, so no type either), reflect.Value.IsZero() checks whether a represented value is the zero value for its type

closing as working as intended

@ianlancetaylor
Copy link
Contributor

To check for the zero value of reflect.Value, use the IsValid method.

@mitar
Copy link
Contributor Author

mitar commented Jan 14, 2021

Why is then language "zero Value" and not "invalid Value"? So language could be:

FieldByName returns the struct field with the given name. It returns the invalid Value if no field was found. It panics if v's Kind is not struct.

@ianlancetaylor
Copy link
Contributor

The reflect documentation consistently refers to the "zero Value". For example, the docs for IsValid say "It returns false if v is the zero Value."

@mitar
Copy link
Contributor Author

mitar commented Jan 14, 2021

It also says: "Most functions and methods never return an invalid Value." :-) So both terminologies are used. And I think one would be less confusing than the other. Especially because "zero" is used also in other contexts.

I understand that now that I know this I can find it and understand what it means. But both me and my co-reviewer at my company both thought that here there should be IsZero and not IsValid (I first used IsZero and then fixed it when code failed, he reviewed it and said he thinks there should be IsZero based on documentation). So, I am reporting that documentation is confusing based on my sample of 2.

@golang golang locked and limited conversation to collaborators Jan 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants