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

encoding/json: add an example for InputOffset() function #48277

Open
Windsooon opened this issue Sep 9, 2021 · 3 comments
Open

encoding/json: add an example for InputOffset() function #48277

Windsooon opened this issue Sep 9, 2021 · 3 comments

Comments

@Windsooon
Copy link

@Windsooon Windsooon commented Sep 9, 2021

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

$ go go1.16.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you expect to see?

Add an example for it.

@Windsooon
Copy link
Author

@Windsooon Windsooon commented Sep 9, 2021

We can reuse the example from Decoder, the new example looks like:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"strings"
)

func main() {
	const jsonStream = `
	{"Name": "Ed", "Text": "Knock knock."}
	{"Name": "Sam", "Text": "Who's there?"}
	{"Name": "Ed", "Text": "Go fmt."}
	{"Name": "Sam", "Text": "Go fmt who?"}
	{"Name": "Ed", "Text": "Go fmt yourself!"}
`
	type Message struct {
		Name, Text string
	}
	dec := json.NewDecoder(strings.NewReader(jsonStream))
	var m Message
	err := dec.Decode(&m);
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s: %s\n", m.Name, m.Text)
        // print the input offset
	fmt.Println(dec.InputOffset())
}

@ameowlia
Copy link
Contributor

@ameowlia ameowlia commented Sep 9, 2021

Were you planning to submit a fix @Windsooon ? If not, I can do this.

@dsnet
Copy link
Member

@dsnet dsnet commented Sep 9, 2021

Let's hold off on adding an example, until we resolve #42571.

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

Successfully merging a pull request may close this issue.

None yet
4 participants