Michael,

There is a third error. In Go, check for errors. Check for scanner errors.

scanner := bufio.NewScanner(ireader)
lcounter := 0
for scanner.Scan() {
    lcounter++
}
if err := scanner.Err(); err != nil {
    if err != io.EOF {
        log.Fatal(err)
    }
}

Output:

2017/06/21 15:13:00 invalid argument

The invalid argument error is triggered by the nil value of f.

Peter

On Wednesday, June 21, 2017 at 2:28:56 PM UTC-4, peterGo wrote:
>
> Michael,
>
> func openFile(s string, file *os.File) {
>     file, err := os.Open(s)
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer file.Close()
> }
>
> There are two errors.
>
> When openfile returns file is closed and it is no longer valid.
>
> In Go, all arguments are passed by value, When openFile returns the value 
> of file is discarded. openFile(fname, f) in main does not change the value 
> of f.
>
> Peter
>
> On Wednesday, June 21, 2017 at 4:58:58 PM UTC, Michael Dwyer wrote:
>>
>> Peter,
>>
>> Thank you for your input regarding my question.
>>
>> In an effort to clear up any confusion or ambiguity regarding the problem 
>> I am running into,
>> I will post the code for what is working and the code that is not working.
>>
>> The following block of code works, a valid file is passed to the 
>> executable, the file is read
>> and the results of how many lines were read are emitted to stdout:
>>
>> package main
>>  
>> import (
>> "flag"
>> "bufio"
>> "fmt"
>> "log"
>> "os"
>> )
>>
>> func cli() string {
>> flag.Parse()
>> return flag.Arg(0)
>> }
>>
>> func openFile(s string, file *os.File) {
>> file, err := os.Open(s)
>> if err != nil {
>> log.Fatal(err)
>> }
>>     defer file.Close()
>> }
>>
>> func main() {
>> fname := cli()
>> f, err := os.Open(fname)
>> if err != nil {
>> log.Fatal(err)
>> }
>> defer f.Close()
>> ireader := bufio.NewReader(f)
>> scanner := bufio.NewScanner(ireader)
>> lcounter := 0
>> for scanner.Scan() {
>> lcounter++
>> }
>> fmt.Printf("# lines read : %d\n", lcounter)
>> }
>>
>> The following is an attempt to refactor the code by moving one section of 
>> code,
>> specifically the code that opens a file handle returning a file 
>> descriptor to variable named "f".
>> The file file descriptor "f" will then be used to create a buffered 
>> Reader & Scanner. It is this code
>> that is not working, for which I seek assistance to understand why it is 
>> not working, and for a solution
>> that adheres to what I was trying to do in the function named openFile(), 
>> which is pass two parameters
>> to openFile(), the first, a string containing the path to be opened, the 
>> second parameter will be a pointer
>> to os.File.
>>
>> What I am attempting to do here is to pass the path to be opened, while 
>> at the same time pass a pointer
>> that will be used to return the file descriptor. The code that is not 
>> working follows:
>>
>> package main
>>  
>> import (
>> "flag"
>> "bufio"
>> "fmt"
>> "log"
>> "os"
>> )
>>
>> func cli() string {
>> flag.Parse()
>> return flag.Arg(0)
>> }
>>
>> func openFile(s string, file *os.File) {
>> file, err := os.Open(s)
>> if err != nil {
>> log.Fatal(err)
>> }
>>     defer file.Close()
>> }
>>
>> func main() {
>> fname := cli()
>> var f *os.File
>> openFile(fname, f)
>> ireader := bufio.NewReader(f)
>> scanner := bufio.NewScanner(ireader)
>> lcounter := 0
>> for scanner.Scan() {
>> lcounter++
>> }
>> fmt.Printf("# lines read : %d\n", lcounter)
>> }
>>
>> I hope that I have been able to remove the ambiguity from the code,
>> and the intent of what it is I am trying to accomplish is clear.
>>
>> Again, thanks in advance to anyone that can help me resolve this issue.
>>
>>
>> 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