Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "os"
- "fmt"
- )
- // Readln returns a single line (without the ending \n)
- // from the input buffered reader.
- // An error is returned iff there is an error with the
- // buffered reader.
- func Readln(r *bufio.Reader) (string, error) {
- var (isPrefix bool = true
- err error = nil
- line, ln []byte)
- for isPrefix && err == nil {
- line, isPrefix, err = r.ReadLine()
- ln = append(ln, line...)
- }
- return string(ln),err
- }
- // 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.
- func main() {
- // path
- file := "/home/madskills/pro/lora.json"
- // open and error handle
- f, err := os.Open(file)
- if err != nil {
- fmt.Printf("error opening file: %v\n", err)
- os.Exit(1)
- }
- r := bufio.NewReader(f)
- jsonMap := make(map[int]string)
- iterator := 1
- s, e := Readln(r)
- for ; e == nil; iterator++ {
- jsonMap[iterator] = s
- fmt.Println(jsonMap[iterator])
- s, e = Readln(r)
- }
- }
Add Comment
Please, Sign In to add comment