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
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Windsooon
Copy link

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 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 commented Sep 9, 2021

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

@dsnet
Copy link
Member

dsnet commented Sep 9, 2021

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

@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 9, 2021
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants