-1

I have an issue where it seems go is telling me that a folder doesn't exist, when it clearly does.

path, _ := reader.ReadString('\n')
path, err := expand(path)
fmt.Println("Path Expanded: ", path, err)

if err == nil {
    if _, err2 := os.Lstat(path); err2 == nil {
        fmt.Println("Valid Path")
    } else if os.IsNotExist(err2) {
        fmt.Println("Invalid Path")
        fmt.Println(err2)
    } else {
        fmt.Println(err2)
    }
}

The expand function simply translates the ~ to the homeDir.

func expand(path string) (string, error) {
    if len(path) == 0 || path[0] != '~' {
        return path, nil
    }

    usr, err := user.Current()
    if err != nil {
        return "", err
    }
    return filepath.Join(usr.HomeDir, path[1:]), nil
}

If I input the value of ~ it correctly translates it to /home/<user>/ but it ultimately states that the folder does not exist, even though it clearly does, and I know I have access to it, so it doesn't seem to be a permissions thing.

if I try /root/ as the input, I correctly get a permissions error, I am ok with that. But I expect my ~ directory to return "Valid Path"

My error is almost always : no such file or directory

I am on Lubuntu 19.xx and it is a fairly fresh install, I am running this app from ~/Projects/src/Playground/AppName and I am using the bash terminal from vscode.

I have also tried both Lstat and Stat unsuccessfully, not to mention a ton of examples and different ways. I am sure this is some underlying linux thing that I don't understand...

Krum110487
  • 611
  • 1
  • 7
  • 20
  • Possible duplicate of [Expand tilde to home directory](https://stackoverflow.com/q/17609732/608639). – jww Jun 06 '19 at 12:15
  • It may not be needed in my go CLI program, but I was pulling my hair out, so I wanted to eliminate it as one of the issues. I wanted the home account to be sure that I have permissions. I can tell you that if I type in the full path, ignoring the tilda, it still does not work. e.g. "/home/username/" is also not found. – Krum110487 Jun 06 '19 at 12:18
  • Please include full output from your program, so we can see how the expansion is happening. – Jonathan Hall Jun 06 '19 at 12:19
  • "ReadString reads until the first occurrence of delim in the input, returning a string containing the data *up to and including the delimiter*." You never trim the newline. – Peter Jun 06 '19 at 12:23
  • Peter, I was actually just noticing that new line character, I think that may be my problem! – Krum110487 Jun 06 '19 at 12:24
  • It is 100% that issue. Also I want to make note that when I use `os.Lstat("~/")` or `os.Lstat("~")` it didn't find that directory, so I know it is contrary to the statements above, it does seem to require the expand function. if someone could clarify on this so if someone else sees this in the future? – Krum110487 Jun 06 '19 at 12:31

1 Answers1

-1

The answer to this is that I was not trimming the ReadString which used the delimiter of \n, by adding strings.Trim(path, "\n"), it corrected my issue.

Krum110487
  • 611
  • 1
  • 7
  • 20
  • Also, why the down vote? I am honestly curious as I felt it was a good question and the "duplicate" was only a duplicate because I didn't realize that the ReadString included the new line character. I only ask so that i can ask better questions going forward, not to complain, but if I did something poorly, how shall I rectify it? – Krum110487 Jun 06 '19 at 14:15