When I use a literal as the filename in an os.Open statement it works. When 
I use a variable defined as text as the filename, it works. When I read in 
the text filename from stdin, it doesn't work. Why is that? What do I need 
to do to make it work? See attached text file.


-- 
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.
Does anyone know why program 1 works, below, and program 2 doesn't? \the only 
difference - I read in the filename from the console in the one that doesn't 
work
// program 1 - this works:
package main
import (
    "fmt"
    "os"
    "bufio"
)
func main() {
        filename := getFilename()
        fmt.Println("opening:",filename)
//      fmt.Println("opening: sample.txt")
        _ , err := os.Open("sample.txt")
//      _ , err := os.Open(filename)
        if err != nil {
                panic(err)
                }
}
func getFilename()  string {
reader := bufio.NewReader(os.Stdin)
        fmt.Print("Enter file name: ")
        filename, _ := reader.ReadString('\n')  // filename not used
        return filename
}

// program 2 - this doesn't work, gives error shown below:
package main
import (
    "fmt"
    "os"
    "bufio"
)
func main() {
        filename := getFilename()
        fmt.Println("opening:",filename)
//      fmt.Println("opening: sample.txt")
//      _ , err := os.Open("sample.txt")
        _ , err := os.Open(filename)
        if err != nil {
                panic(err)
                }
}
func getFilename()  string {
reader := bufio.NewReader(os.Stdin)
        fmt.Print("Enter file name: ")  // here enter "sample.txt" (without the 
quotes)....
        filename, _ := reader.ReadString('\n')  // filename read in is used
        return filename
}
Error message, panic invoked - 
: The filename, directory name, or volume label syntax is incorrect.

goroutine 1 [running]:
main.main()
        C:/installed programs/go/src/testopen2.go:16 +0x158
exit status 2

Reply via email to