Proposal to check for a file or directory

433 views
Skip to first unread message

Archos

unread,
Nov 24, 2010, 6:24:28 AM11/24/10
to golang-nuts
Since it's very common to check if exist a directory before of make
it, then this function would be very useful in "os" package.

* * *
func Exist(name string) (bool, os.Error) {
_, err := os.Stat(name)
if err == nil {
return true, nil
}

// Check if error is "no such file or directory"
if e, ok := err.(*os.PathError); ok && e.Error == os.ENOENT {
return false, nil
}
return false, err
}
* * *

Wei guangjing

unread,
Nov 24, 2010, 8:13:20 AM11/24/10
to Archos, golang-nuts
2010/11/24 Archos <raul...@sent.com>

Since it's very common to check if exist a directory before of make
it, then this function would be very useful in "os" package.

"path" package should be better.
 

* * *
func Exist(name string) (bool, os.Error) {

Why return error? Just return bool should be better.
 

chris dollin

unread,
Nov 24, 2010, 12:52:11 PM11/24/10
to Wei guangjing, Archos, golang-nuts
On 24 November 2010 13:13, Wei guangjing <vcc...@gmail.com> wrote:
> 2010/11/24 Archos <raul...@sent.com>
>>
>> Since it's very common to check if exist a directory before of make
>> it, then this function would be very useful in "os" package.
>
> "path" package should be better.

I was going to say, why? Because path is all aboiut hacking
paths, not about opening/checking them. Then I discovered
path.Walk. Oops.

So, why is Walk in path? It doesn't seem to belong there.
Maybe ioutil or os?

--
Chris "allusive" Dollin

Wei guangjing

unread,
Nov 25, 2010, 6:40:50 AM11/25/10
to chris dollin, Archos, golang-nuts
2010/11/25 chris dollin <ehog....@googlemail.com>

If in os, will be os.WalkDir, os.Walk walk what? path.Walk is better than os.WalkDir.
 

--
Chris "allusive" Dollin

chris dollin

unread,
Nov 25, 2010, 7:07:10 AM11/25/10
to Wei guangjing, Archos, golang-nuts

os.WalkFileTree

Chris

--
Chris "allusive" Dollin

Russ Cox

unread,
Nov 29, 2010, 1:23:50 PM11/29/10
to golan...@googlegroups.com
path.Walk is in package path because it is a good name
and because it depends on higher level packages than os.
Even things like path.Split and path.Join end up being 
operating system dependent once Windows is involved,
so having Walk doesn't make path any more operating
system dependent.

Russ

Reply all
Reply to author
Forward
0 new messages