Skip to content

Instantly share code, notes, and snippets.

@ikrabbe
Created December 25, 2016 12:14
Show Gist options
  • Save ikrabbe/88f8be4923cd95921e00ca2489a6bf84 to your computer and use it in GitHub Desktop.
Save ikrabbe/88f8be4923cd95921e00ca2489a6bf84 to your computer and use it in GitHub Desktop.
stackoverflow test file
// Package test is a pure testing package
package test
import (
"bytes"
"encoding/json"
"fmt"
)
const data = `
{
"title":"New Year's Resolution",
"name":"What will be your New Year's Resolution?",
"qname":["ques1","ques2","ques3","ques4","ques5","ques6"]
}
`
func ExampleQuiz() {
var Quiz struct { //Design your quiz page using this
Title string `json:"title"`
Name string `json:"name"`
Qname []string `json:"qname"` // removing the [] gives the error
// "parsing file: json: cannot unmarshal array into Go struct field .qname of type string"
}
buf := bytes.NewBufferString(data)
jsonParser := json.NewDecoder(buf)
err := jsonParser.Decode(&Quiz)
if err != nil {
fmt.Printf("parsing file: %s", err.Error()) // I get
} else {
fmt.Println("ok.")
}
// Output: ok.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment