You can do this:
home, _ := os.UserHomeDir()
absPath := home + "/trunk/infra/metadata/config.cfg"
> On Jul 7, 2020, at 4:32 PM, 'Jacob Kopczynski' via golang-nuts
> <[email protected]> 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 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.
>
> What's the process for submitting a change to a core library like this one?
> Ideally, to the code, but I'd settle for making the documentation clearer.
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/ee99cbb9-583f-487f-9e57-e0072d7a859an%40googlegroups.com.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/E7D2DCBD-19F8-4497-AA6E-45E014130613%40iitbombay.org.