I want to get a file path from the console and check if the file is a vm file. I wrote this code:
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter the path of the vm file:")
path, _ := reader.ReadString('
')
if filepath.Ext(path) != ".vm" {
fmt.Println("Error! file must be vm file")
}
but it's not working. I just started learn Go So please accept my apologies if this is a basic mistake.
The path, _ := reader.ReadString(' ')
also returns the newline. So your comparing ".vm " with ".vm".
you should trimspace from the path if filepath.Ext(strings.TrimSpace(path)) != ".vm" {