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

noinit doesn't know about nil slices #82

Closed
kluzzebass opened this issue Feb 13, 2023 · 1 comment · Fixed by #83
Closed

noinit doesn't know about nil slices #82

kluzzebass opened this issue Feb 13, 2023 · 1 comment · Fixed by #83

Comments

@kluzzebass
Copy link

I tried to use noinit with a []byte slice, but the parser complained that field must be a pointer to have noinit. It seems a bit unnecessary to insist on having a pointer to a slice, since a slice in itself can be nil. Consider the following code:

package main

import "fmt"

type Lala struct {
	Blah []byte
}

func main() {
	lala := Lala{}
	if lala.Blah == nil {
		fmt.Println("Blah is nil")
	}
	lala.Blah = []byte{}
	if lala.Blah != nil {
		fmt.Println("Blah is not nil")
	}
}

The result is:

Blah is nil
Blah is not nil
sethvargo added a commit that referenced this issue Feb 13, 2023
Previously, this would throw an error. Fixes GH-82.
@sethvargo
Copy link
Owner

#83

Note this isn't possible with Arrays, because Arrays must be allocated (enforced by the compiler).

sethvargo added a commit that referenced this issue Feb 13, 2023
Previously, this would throw an error. Fixes GH-82.
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 a pull request may close this issue.

2 participants