Ya no necesitas investigar más por internet ya que has llegado al espacio necesario, poseemos la respuesta que buscas pero sin complicarte.
Ejemplo 1: golang lee el archivo línea por línea
package main
import("bufio""bytes""fmt""io""os")funcreadFileWithReadString(fn string)(err error)
fmt.Println("readFileWithReadString")
file, err := os.Open(fn)defer file.Close()if err !=nilreturn err
// Start reading from the file with a reader.
reader := bufio.NewReader(file)var line stringfor
line, err = reader.ReadString('n')
fmt.Printf(" > Read %d charactersn",len(line))// Process the line here.
fmt.Println(" > > "+limitLength(line,50))if err !=nilbreakif err != io.EOF
fmt.Printf(" > Failed!: %vn", err)returnfuncreadFileWithScanner(fn string)(err error)
fmt.Println("readFileWithScanner - this will fail!")// Don't use this, it doesn't work with long lines...
file, err := os.Open(fn)defer file.Close()if err !=nilreturn err
// Start reading from the file using a scanner.
scanner := bufio.NewScanner(file)for scanner.Scan()
line := scanner.Text()
fmt.Printf(" > Read %d charactersn",len(line))// Process the line here.
fmt.Println(" > > "+limitLength(line,50))if scanner.Err()!=nil
fmt.Printf(" > Failed!: %vn", scanner.Err())returnfuncreadFileWithReadLine(fn string)(err error)
fmt.Println("readFileWithReadLine")
file, err := os.Open(fn)defer file.Close()if err !=nilreturn err
// Start reading from the file with a reader.
reader := bufio.NewReader(file)forvar buffer bytes.Buffer
var l []bytevar isPrefix boolfor
l, isPrefix, err = reader.ReadLine()
buffer.Write(l)// If we've reached the end of the line, stop reading.if!isPrefix break// If we're just at the EOF, breakif err !=nilbreakif err == io.EOF break
line := buffer.String()
fmt.Printf(" > Read %d charactersn",len(line))// Process the line here.
fmt.Println(" > > "+limitLength(line,50))if err != io.EOF
fmt.Printf(" > Failed!: %vn", err)returnfuncmain()testLongLines()testLinesThatDoNotFinishWithALinebreak()functestLongLines()
fmt.Println("Long lines")
fmt.Println()createFileWithLongLine("longline.txt")readFileWithReadString("longline.txt")
fmt.Println()readFileWithScanner("longline.txt")
fmt.Println()readFileWithReadLine("longline.txt")
fmt.Println()functestLinesThatDoNotFinishWithALinebreak()
fmt.Println("No linebreak")
fmt.Println()createFileThatDoesNotEndWithALineBreak("nolinebreak.txt")readFileWithReadString("nolinebreak.txt")
fmt.Println()readFileWithScanner("nolinebreak.txt")
fmt.Println()readFileWithReadLine("nolinebreak.txt")
fmt.Println()funccreateFileThatDoesNotEndWithALineBreak(fn string)(err error)
file, err := os.Create(fn)defer file.Close()if err !=nilreturn err
w := bufio.NewWriter(file)
w.WriteString("Does not end with linebreak.")
w.Flush()returnfunccreateFileWithLongLine(fn string)(err error)
file, err := os.Create(fn)defer file.Close()if err !=nilreturn err
w := bufio.NewWriter(file)
fs :=1024*1024*4// 4MB// Create a 4MB long line consisting of the letter a.for i :=0; i < fs; i++
w.WriteRune('a')// Terminate the line with a break.
w.WriteRune('n')// Put in a second line, which doesn't have a linebreak.
w.WriteString("Second line.")
w.Flush()returnfunclimitLength(s string, length int)stringiflen(s)< length return s
return s[:length]
Ejemplo 2: línea de lectura de golang
package main
import"fmt"funcmain()
x :=string
fmt.Scanln(&x)
Te invitamos a estimular nuestra faena exponiendo un comentario o dejando una puntuación te damos la bienvenida.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)