Skip to content

Instantly share code, notes, and snippets.

@ueokande
Created June 6, 2019 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueokande/33ba62e620a5e314a9ee540d36029cb4 to your computer and use it in GitHub Desktop.
Save ueokande/33ba62e620a5e314a9ee540d36029cb4 to your computer and use it in GitHub Desktop.
Convert tab-splitted dictionaly to json
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
)
func run(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
dic := make(map[string]string)
for s.Scan() {
line := s.Text()
words := strings.Split(line, "\t")
if len(words) != 2 {
return errors.New("syntax error: " + line)
}
dic[words[0]] = words[1]
}
err = s.Err()
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(dic)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "DICT_FILE")
os.Exit(1)
}
err := run(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment