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

Clarify the role of io.EOF in Decode #1015

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from

Conversation

lukemassa
Copy link

I was originally confused by the fact that this code produced an error:

	var test struct{}
	err := yaml.NewDecoder(bytes.NewReader([]byte(""))).Decode(&test)
	fmt.Println(test, err)

whereas this did not:

	var test struct{}
	err := yaml.Unmarshal([]byte(""), &test)
	fmt.Println(test, err)

See: https://go.dev/play/p/tM_jp90fjFx

However, when I looked closer, I realized that this was the intended behavior. Decode() decodes the "next" document until it can't find any more, which it then indicates via io.EOF. If it were to return nil, there'd be no way for the client of Decode() to know there were no more documents.

This change adds to the documentation so this is a bit clearer.

Closes: #805

// and stores it in the value pointed to by v. If there are
// no more yaml documents to decode (or there never was, for
// example if the Decoder's io.Reader never had any bytes),
// it will return an io.EOF
Copy link

@dolmen dolmen Feb 21, 2024

Choose a reason for hiding this comment

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

Please use godoc links and end the sentence with a dot.

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.

Reading empty file causes Decode to return error
2 participants