path.Exists

3,272 views
Skip to first unread message

Andrei Simionescu

unread,
Mar 17, 2014, 7:09:02 PM3/17/14
to golan...@googlegroups.com
I think a method path.Exists(path string) bool would be a nice thing to have – newbies like me will probably find this snippet of code [0] and implement the same helper method, anyway.

Pros: Makes the language more newbie-friendly, thus potentially increasing adoption
Cons: ??

I can imagine language adoption being low on the priority list, but I don't see how adding this would hurt. Opinions?

Thanks (and sorry if this has been discussed before)

Thomas Bushnell, BSG

unread,
Mar 17, 2014, 7:14:19 PM3/17/14
to Andrei Simionescu, golang-nuts
There are more than one error which can happen from os.Stat. So you'd need

func Exists(string) (error, bool)

And at that point, the calls are no shorter with this method than with os.Stat. (Though perhaps a little more 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 golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caleb Spare

unread,
Mar 17, 2014, 7:14:53 PM3/17/14
to Andrei Simionescu, golang-nuts
This has been discussed before a few times. I think the conclusion is
that it's not actually needed very often and that using os.Stat is
easy enough for the cases where it is required.

Note that newcomers often feel that they need an os.Exists function
before learning of a better way to do what they're trying to do. For
instance: if you are going to open the file, there's no reason to
check whether it exists first. The file could disappear in between
checking and opening, and anyway you'll need to check the os.Open
error regardless. So you simply call os.IsNotExist(err) after you try
to open the file, and deal with its non-existence there (if that
requires special handling).

-Caleb

On Mon, Mar 17, 2014 at 4:09 PM, Andrei Simionescu
<ansimi...@gmail.com> wrote:

Andrei Simionescu

unread,
Mar 17, 2014, 7:19:59 PM3/17/14
to golan...@googlegroups.com
Thanks for the quick replies. An (bool, err) version is better, yes.

@Caleb: I am trying to check whether some config files and dirs have been already created – see link

Caleb Spare

unread,
Mar 17, 2014, 7:32:52 PM3/17/14
to Andrei Simionescu, golang-nuts
Actually, your code has exactly the issues I was describing. You don't
need to check for the paths existing at all (and you shouldn't).

- os.MkdirAll works whether or not the paths already exist. (Also you
need to check the error from that call.)

- Instead of using os.Create, you should use os.OpenFile(path,
os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666). That way you'll get an error
if the file already exists. Also this doesn't have a race condition
with something else making the file, unlike your version which checks
for existence beforehand.

-Caleb

OscarRyz

unread,
Mar 18, 2014, 10:46:26 AM3/18/14
to golan...@googlegroups.com, Andrei Simionescu
This should be the accepted answer in stackoverflow. 

If you don't mind I'll rewrite it there.

OscarRyz

unread,
Mar 18, 2014, 2:57:11 PM3/18/14
to golan...@googlegroups.com, Andrei Simionescu
Reply all
Reply to author
Forward
0 new messages