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

Always ignore _ fields #108

Merged
merged 2 commits into from
Feb 17, 2019
Merged

Always ignore _ fields #108

merged 2 commits into from
Feb 17, 2019

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Feb 17, 2019

The _ field in Go is special in that it cannot be read from or written to.
Thus, skip it by default. It is pointless to require the user to use
cmpopts.IgnoreUnexported just for the _ field.

The _ field in Go is special in that it cannot be read from or written to.
Thus, skip it by default. It is pointless to require the user to use
cmpopts.IgnoreUnexported just for the _ field.
Copy link

@cybrcodr cybrcodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware that it is possible to have blank identifier as a field name. I wonder why the language provides such construct, seems not useful.

@dsnet
Copy link
Collaborator Author

dsnet commented Feb 17, 2019

It's useful when dealing with unsafe and you want to explicitly control the memory layout of the struct.

For example:

type Foo struct {
     _ uint64
     v uint64 // lives at offset +8B
}

Or to nest a type to explicitly make the type incomparable:

type Incomparable struct {
    _ [0]func()
   ... // other fields
}

@dsnet dsnet merged commit 19e9c26 into master Feb 17, 2019
@dsnet dsnet deleted the underscore-field branch February 17, 2019 05:14
@cybrcodr
Copy link

Neat.

I didn't know that _ field will take up space of given type (unless 0).

Good to know of another way to make a struct incomparable apart from embedding a func type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants