Impossible to scan a filepath with Golang

Issue

I am creating a client with the Cobra framework in Go that consists of uploading a file to a transfer service that will be released very soon.
I want it to be a minimally customizable with a default download folder.

Problem when I want to check that a folder exists, that the folder is a folder and not a file or check that I have permission to write to the folder I get a huge goroutine error.

To know:
I use drag and drop in macOS

var dldpath string
inquire.Query().Input(&dldpath, "Chemin du dossier de téléchargement ", nil).Exec()

println("le chemin est : ", dldpath)

if len(dldpath) == 0 {
    fmt.Println(chalk.Red, "Erreur : Vous devez spécifier un dossier à téléverser")
    os.Exit(0)
}
dir, err := os.Stat(dldpath)

if !dir.IsDir() {
    fmt.Println(chalk.Red, "Erreur : Le chemin spécifié n'est pas un dossier")
    os.Exit(0)
}

if errors.Is(err, os.ErrNotExist) {
    fmt.Println(chalk.Red, "Erreur : Dossier introuvable")
    os.Exit(0)
}


screen.Clear()
screen.MoveTopLeft()
fmt.Println(chalk.Green, "Dossier de téléchargement par défaut changé avec succès")

Error : (sorry I can’t upload images)

le chemin est :  '/Users/eliezayat/Downloads/bassine wavy.png'
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x119b260]

goroutine 1 [running]:
freetranscli/cmd.glob..func3(0x1370900?, {0x11ef012?, 0x0?, 0x0?})
        /Users/eliezayat/Documents/freetranscli/cmd/set.go:44 +0x280
github.com/spf13/cobra.(*Command).execute(0x1370900, {0x13a3d80, 0x0, 0x0})
        /Users/eliezayat/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 +0x847
github.com/spf13/cobra.(*Command).ExecuteC(0x1370620)
        /Users/eliezayat/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/eliezayat/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968
freetranscli/cmd.Execute()
        /Users/eliezayat/Documents/freetranscli/cmd/root.go:28 +0x25
main.main()
        /Users/eliezayat/Documents/freetranscli/main.go:8 +0x17
exit status 2

Solution

In case if dir variable is nil you’ll receive nil pointer dereference.

It’s better to check for error (errors.Is) before check for dir(dir.IsDir()):

dir, err := os.Stat(dldpath)
if errors.Is(err, os.ErrNotExist) {
    fmt.Println(chalk.Red, "Erreur : Dossier introuvable")
    os.Exit(0)
}
if !dir.IsDir() {
    fmt.Println(chalk.Red, "Erreur : Le chemin spécifié n'est pas un dossier")
    os.Exit(0)
}

Answered By – Philidor

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.