Guest User

Untitled

a guest
Apr 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "os"
  6. "fmt"
  7. )
  8.  
  9. // Readln returns a single line (without the ending \n)
  10. // from the input buffered reader.
  11. // An error is returned iff there is an error with the
  12. // buffered reader.
  13. func Readln(r *bufio.Reader) (string, error) {
  14. var (isPrefix bool = true
  15. err error = nil
  16. line, ln []byte)
  17. for isPrefix && err == nil {
  18. line, isPrefix, err = r.ReadLine()
  19. ln = append(ln, line...)
  20. }
  21. return string(ln),err
  22. }
  23.  
  24. // You can use Readln to read every line from a file. The following code reads every line in a file and outputs each line to stdout.
  25. func main() {
  26. // path
  27. file := "/home/madskills/pro/lora.json"
  28. // open and error handle
  29. f, err := os.Open(file)
  30. if err != nil {
  31. fmt.Printf("error opening file: %v\n", err)
  32. os.Exit(1)
  33. }
  34.  
  35. r := bufio.NewReader(f)
  36. jsonMap := make(map[int]string)
  37.  
  38. iterator := 1
  39. s, e := Readln(r)
  40.  
  41. for ; e == nil; iterator++ {
  42. jsonMap[iterator] = s
  43. fmt.Println(jsonMap[iterator])
  44. s, e = Readln(r)
  45. }
  46. }
Add Comment
Please, Sign In to add comment