can anyone figure this out? I think the problem might be in the readstring() argument when being passed to the create file function. I tried resolving it by stripping ' ' but the error remains. Is there a workaround other than readstring()? Because I think it's the one causing issues. Thank you
package main
import (
"fmt"
"bufio"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('
')
text = strings.TrimSuffix(text, "
")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:"+text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string){
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Ok for those who are new to this problem, I've manage to find a workaround using scanf. So consider this problem solved.
Start with basic debugging techniques: check for errors and check input.
Replace
text, _ := reader.ReadString('
')
with
text, err := reader.ReadString('
')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q
", text)
For example,
so1.go
:
package main
import (
"bufio"
"fmt"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('
')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q
", text)
text = strings.TrimSuffix(text, "
")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:" + text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string) {
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Output:
>go run so1.go
Enter filename to create: test.file
"test.file
"
Name:test.file
test.file
: The filename, directory name, or volume label syntax is incorrect.
exit status 1
On Windows, the line is terminated with "".
Replace
text = strings.TrimSuffix(text, "
")
with
text = strings.TrimSuffix(text, "
")
text = strings.TrimSuffix(text, "")
For example,
so2.go
:
package main
import (
"bufio"
"fmt"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('
')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q
", text)
text = strings.TrimSuffix(text, "
")
text = strings.TrimSuffix(text, "")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:" + text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string) {
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Output:
>go run so2.go
Enter filename to create: test.file
"test.file
"
Name:test.file
test.file
Now that the program works, remove this debugging statement:
fmt.Printf("%q
", text)