If I try to read the file name through the terminal go can't seem to find it. But if I hard code it everything works out fine? this isn't a problem with writing out.
This code:
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os")
func check(e error) {
if e != nil {
panic(e)
}
}
func getUserInput(message string) (text string){
reader := bufio.NewReader(os.Stdin)
fmt.Println(message)
text, err := reader.ReadString('
')
check(err)
return text
}
func main() {
input := getUserInput("File to open?")
fmt.Println(input)
dat, err := ioutil.ReadFile(input)
check(err)
fmt.Print("% x
", dat)
input = getUserInput("File to write?")
d1 := []byte(dat)
e := ioutil.WriteFile(input, d1, 0644)
check(e)
}
Yields:
panic: open a.jpg
: no such file or directory
goroutine 1 [running]:
runtime.panic(0x4a36c0, 0xc21001d2a0)
/usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6
main.check(0x7fdcd2e07088, 0xc21001d2a0)
/home/matt/Dropbox/CSE3320/fs_GO/fs.go:17 +0x4f
main.main()
/home/matt/Dropbox/CSE3320/fs_GO/fs.go:35 +0x13e
exit status 2
yet this code:
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os")
func check(e error) {
if e != nil {
panic(e)
}
}
func getUserInput(message string) (text string){
reader := bufio.NewReader(os.Stdin)
fmt.Println(message)
text, err := reader.ReadString('
')
check(err)
return text
}
func main() {
//input := getUserInput("File to open?")
//fmt.Println(input)
dat, err := ioutil.ReadFile("a.jpg")
check(err)
//fmt.Print("% x
", dat)
input := getUserInput("File to write?")
d1 := []byte(dat)
e := ioutil.WriteFile(input, d1, 0644)
check(e)
}
When running the first code without checking for error a blank new file is created. Yet the second run works perfectly fine even writing the file with the same function to get the new file name.
I'm completely lost as to what I'm doing wrong here. Here is my version info if that helps let me know what else you need.
go version
gives me go version go1.2.1 linux/amd64
.