On Wed, Jul 8, 2020 at 2:44 AM 'Jacob Kopczynski' via golang-nuts < golang-nuts@googlegroups.com> wrote:
> I tried to state a path relative to the home directory, since in the > context I am writing for the path from ~ to the Git repository will be > consistent, though the meaning of ~ may change based on user. A snippet: > > const specPath = "~/trunk/infra/metadata/config.cfg" > absPath, _ := filepath.Abs(specPath) > if err := readJSONPb(absPath, &s); err != nil { > fmt.Printf("specPath is %s, or as absolute %s\n", specPath, absPath) > return nil, errors.Annotate(err, "extracting tests from spec").Err() > } > However, this produces this output: > specPath is ~/trunk/infra/metadata/config.cfg, or as absolute > /home/jkop/trunk/infra/~/trunk/infra/metadata/config.cfg > extracting tests from spec: read JSON pb: open > /home/jkop/trunk/infra/~/trunk/infra/metadata/config.cfg: no such file or > directory > > This is a) surprising, since I would expect a platform-sensitive absolute > path function to deal with platform-global shortcuts like ~, and > ~ is not a "platform global shortcut". It's an expansion a shell (bash, zsh, etc) does for convenience. open() and similar C-level (or Go level etc) function are not opening files through a shell. > b) completely undocumented. The docs say that "Abs returns an absolute > representation of path. If the path is not absolute it will be joined with > the current working directory to turn it into an absolute path." It doesn't > say "If the path is not *rooted*", but "not *absolute*". This is not an > absolute *representation* but it does specify an absolute path > unambiguously. > Not really unambiguously. Consider a function like: listDirContents("~") Is "~" a file/dir called "~" in the current working path (which is totally legal to create) or "/home/<current user>"? Note that this is how almost all standard libraries behave, except some that have expansion out of convenience, in higher level languages (not something like C or Go). Even Python, which has all shorts of conveniences, doesn't expand tildes: >>> open("~") > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > IOError: [Errno 2] No such file or directory: '~' And the equilavent abspath treats the "~" as a "~" file in the local dir (/Users/username/ was not expanded from ~ -- I just happened to run the command from there): >>> import os > >>> os.path.abspath("~") > '/Users/username/~' > >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAACdnTDoX2b0%2B-vfRFS0MzpggtjzWVqFhE0cJsibyTmNMEBF4g%40mail.gmail.com.