Last active
August 13, 2018 18:15
-
-
Save hectorgool/6ea5f8f2569cd33cbf82fad3066b14da to your computer and use it in GitHub Desktop.
Se tiene un archivo txt y se quiere leer su contenido para procesarlo.
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
hola |
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
/* | |
go run leer_archivos1.go | |
*/ | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
) | |
const CHAR = "\n" | |
func main() { | |
//ReadFile regresa un slice de bytes | |
caracteres, err := ioutil.ReadFile("hola.txt") // como argumento se pone el nombre del archivo | |
if err != nil { | |
fmt.Print(err) | |
} | |
fmt.Printf("El arreglo de byes es:\n %v \n", caracteres) // caracteres es donde se guarda el slice de bytes | |
//se recorre uno a uno los elementos del slice con range | |
//cada uno de los elementos del slice se guarda en la variable c que es de typo byte | |
//con la función string el caracter byte se pasa a typo string | |
for _, c := range caracteres { | |
fmt.Printf("El caracter tipo byte: %v, a tipo string es %#v\n", c, string(c)) | |
} | |
//Otra opción es aplicar la función string al slice, para convertir el contenido a cadenas de texto | |
fmt.Printf("\n%v\n", string(caracteres)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment