[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread Michael Dwyer
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 inte

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread Michael Dwyer
Peter, Thank you for helping me out. I'm going to be busy the next couple of days. When I have some free time I will go over your replies. Seems that my analysis was inadequate. I do appreciate the points you made. Gives me an opportunity to try some different code tweaks. When I am able I will

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread peterGo
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 in

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread peterGo
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 r

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread peterGo
Michael, In Go, all arguments are passed by value. Therefore the value of file is discarded at the end of openfile. In main, f remains nil. 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 t

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread peterGo
Michael, Your first post was clear. Therefore the error remains. When you close the file you lose the fd. f is nil. For example, package main import ( "bufio" "flag" "fmt" "log" "os" ) func cli() string { flag.Parse() return flag.Arg(0) } func openFile(s string, fi

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread Michael Dwyer
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 execut

[go-nuts] Re: Problems refactoring call to Open()

2017-06-21 Thread peterGo
Michael, func openFile(s string, file *os.File) { file, err := os.Open(s) if err != nil { log.Fatal(err) } defer file.Close() } You open the file and when the function exits, close the file. Peter On Wednesday, June 21, 2017 at 12:44:36 PM UTC, Michael Dwyer wrote: > > Hello to all, > > My