Samstag, 28. November 2009

read a file and print the count of lines


package main

import (
"io";
"fmt";
"os";
"strings";
)

func main() {
// read the file in bytes array
b, err := io.ReadFile("readfile.go");
if err!=nil {
fmt.Println("error on read file");
os.Exit(-1);
}

// convert from bytes to string
strbuffer := string(b);

// split into lines
lines := strings.Split(strbuffer, "\n", 0);

// iterate each
for i, line := range lines {
fmt.Printf("%d: %s\n", i, line);
}
}

Keine Kommentare:

Kommentar veröffentlichen