Peter,

with your responses I was able to figure out the cause of the error. I 
created a function that calls defer.
I did it as a function as I put some trace code in along with the Close(). 
>From that I concluded you were correct.
I went ahead and reworked the rest of the code, which was my intent for 
this particular exercise.

I have other questions, not related to this topic, so I won't talk about 
that, subject for another topic,
and only after I have performed the necessary research. I do want to thank 
you for your help, much appreciated ...

Following is the corrected, updated code ...

package main
 
import (
"flag"
"bufio"
"fmt"
"log"
"os"
)

func cli() string {
flag.Parse()
return flag.Arg(0)
}

func openFile(s string) *os.File {
file, err := os.Open(s)
if err != nil {
log.Fatal(err)
}

return file
}

func ClosingFile(file *os.File) {
file.Close()
}

func CreateScanner(file *os.File) *bufio.Scanner {
ireader := bufio.NewReader(file)
scanner := bufio.NewScanner(ireader)

return scanner
}

func CountLines(scanner *bufio.Scanner) int {
var count int

for scanner.Scan() {
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
count++
}
return count
}

func main() {
fname := cli()

f := openFile(fname)
defer ClosingFile(f)

s := CreateScanner(f)

lcounter := 0
lcounter = CountLines(s)

fmt.Printf("# lines read : %d\n", lcounter)
}


THANX(MKD).

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to