Created
October 24, 2018 12:53
-
-
Save danielvaughan/fb32abd2101935a02e1513c54519a0f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"compress/gzip" | |
"encoding/json" | |
"fmt" | |
"log" | |
"os" | |
) | |
func main() { | |
//This struct acts as a model for the JSON you are reading | |
type Sample struct { | |
Name string `json:"name"` | |
Accession string `json:"accession"` | |
} | |
inputFile := os.Args[1] | |
f, err := os.Open(inputFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
gz, err := gzip.NewReader(f) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
defer gz.Close() | |
dec := json.NewDecoder(gz) | |
// read open bracket | |
_, err = dec.Token() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// while the array contains values | |
for dec.More() { | |
var s Sample | |
// decode an array value (Sample) | |
err := dec.Decode(&s) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%v:%v\n", s.Name, s.Accession) | |
} | |
// read closing bracket | |
_, err = dec.Token() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment