Does the file exist or not!?

Report a typo

Lena has just finished learning about how to interact with the OS and get file attributes in Go.

She wants to create a very simple Go program that takes as an input the name of a file or a directory and checks if it exists or not within her project workspace. However, she forgot what functions from the os and the errors package you can use to check if a file exists!

Please help Lena write the missing code line with the correct function that checks if the file exists or not within her project directory.

Remember to import the os or the errors package to properly implement the file's existence check!

Sample Input 1:

main.go

Sample Output 1:

main.go exists!
Write a program in Go
package main

import (
"fmt"
"os"
)

func main() {
var fileName string
fmt.Scanln(&fileName)

fileInfo, err := os.Stat(fileName)
// Write the correct function to check if the file exists or not below!
if ?(?) {
fmt.Println(fileName, "does not exist!")
return
}
if err != nil {
return
}
fmt.Println(fileInfo.Name(), "exists!")
}
___

Create a free account to access the full topic