path.Ext returns the dot

1,798 views
Skip to first unread message

Archos

unread,
Aug 29, 2010, 5:50:02 AM8/29/10
to golang-nuts
The extension of a file is the substring which follows the last dot
character (e.g. "txt" is the extension of "readme.txt"). But
`path.Ext()` is returning the dot (e.g. ".txt").

When I'm going to compare against an extension I expect to use only
its extension (without the dot.)


http://golang.org/pkg/path/#Ext

Joseph Poirier

unread,
Aug 29, 2010, 12:31:47 PM8/29/10
to Archos, golang-nuts
> When I'm going to compare against an extension I expect to use only
> its extension (without the dot.)

I believe common convention is to include the dot.

-joe

distributed

unread,
Aug 30, 2010, 8:06:07 AM8/30/10
to golang-nuts
I quote from http://golang.org/pkg/path/#Ext:

> Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final slash-separated element of path;

That clearly states that the dot is part of what Ext returns. I would
also agree that it is common to include the dot in the extension.

Russ Cox

unread,
Aug 30, 2010, 10:27:14 AM8/30/10
to Archos, golang-nuts
On Sun, Aug 29, 2010 at 02:50, Archos <raul...@sent.com> wrote:
> The extension of a file is the substring which follows the last dot
> character (e.g. "txt" is the extension of "readme.txt"). But
> `path.Ext()` is returning the dot (e.g. ".txt").
>
> When I'm going to compare against an extension I expect to use only
> its extension (without the dot.)

After reading the documentation I hope you now expect the dot too.
I bet this isn't the first thing about Go that was different than what
you expected.

Including the dot in the return value allows path.Ext to distinguish
between the inputs "foo." and "foo".

Russ

Archos

unread,
Aug 30, 2010, 12:02:56 PM8/30/10
to golang-nuts

On Aug 30, 2:27 pm, Russ Cox <r...@golang.org> wrote:
> On Sun, Aug 29, 2010 at 02:50, Archos <raul....@sent.com> wrote:
> > The extension of a file is the substring which follows the last dot
> > character (e.g. "txt" is the extension of "readme.txt"). But
> > `path.Ext()` is returning the dot (e.g. ".txt").
>
> > When I'm going to compare against an extension I expect to use only
> > its extension (without the dot.)
>
> After reading the documentation I hope you now expect the dot too.
> I bet this isn't the first thing about Go that was different than what
> you expected.
If I wrote the definition of extension was to note that isn't
consistent with that function.

http://en.wikipedia.org/wiki/Filename_extension

Joseph Poirier

unread,
Aug 30, 2010, 2:13:09 PM8/30/10
to Archos, golang-nuts
> If I wrote the definition of extension was to note that isn't
> consistent with that function.
>
> http://en.wikipedia.org/wiki/Filename_extension

I wouldn't consider wikipedia as the normative source.
Informative yes, but not normative.

But even if you ignore the documentation's function description,
it is consistent with that function, but I guess it depends on
what you're expecting based on the language/s you've used.

Eg, the Python and Ruby built-in functions include the dot whereas
(I can see that) PHP and C# don't.

-joe

python
---------
>> import os
>> file = "/this/file/extension/contains/a/dot/file.here"
>> ext = os.path.splitext(file)[1]
>> print ext
output> .here

ruby
------
file = "/this/file/extension/contains/a/dot/file.here"
ext = File.extname(file)
puts ext
output> .here

php
-----
$file = "/this/file/extension/has/no/dot/file.nodot"
$ext = pathinfo($file, PATHINFO_EXTENSION)
echo $ext
output> nodot

C#
----
string file = "/this/file/extension/has/no/dot/file.nodot"
string ext = Path.GetExtension(file)
System.Console.WriteLine(ext)
output> nodot

Otan

unread,
Sep 21, 2010, 4:25:17 AM9/21/10
to golang-nuts
Speaking of extension name,

fmt.Println(path.Ext("file."))
gives me a .

In Go, 'file.' has an extension name of '.' instead of empty string.

Hmmm. I remember my professor and the books I've read in college say
that "a file name has a base name and an optional extension name
delimited by the last ." ===> base.ext <===

Weird but not a big deal really, as long as the behavior is
documented. And I'm content and happy with whatever Go maintainers
decide. :-D They're the geniuses and they know what's best, my heroes.
>:d<

Joseph Poirier

unread,
Sep 21, 2010, 5:04:36 AM9/21/10
to Otan, golang-nuts
On Tue, Sep 21, 2010 at 3:25 AM, Otan <trutho...@gmail.com> wrote:
> Speaking of extension name,
>
> fmt.Println(path.Ext("file."))
> gives me a .
>
> In Go, 'file.' has an extension name of '.' instead of empty string.
>
> Hmmm. I remember my professor and the books I've read in college say
> that "a file name has a base name and an optional extension name
> delimited by the last ."   ===> base.ext <===
>

The file extension in your example /does/ have an empty string after the
delimiter. Remember that some languages include the dot with the returned
extension and some don't, Go does.

ext := path.Ext("file.")
if ext != "" {
// not empty therefore at least the dot delimiter exists
if ext[1:] == "" {
fmt.Println("ext is an empty string")
} else {
fmt.Println("ext is ", ext[1:])
}
} else {
fmt.Println("the file has no extension")
}

-joe

Russ Cox

unread,
Sep 21, 2010, 6:13:22 AM9/21/10
to Otan, golang-nuts
> Hmmm. I remember my professor and the books I've read in college say
> that "a file name has a base name and an optional extension name
> delimited by the last ."   ===> base.ext <===

Sounds like MS-DOS to me.
Other systems distinguish 'base.' from 'base',
and path.Ext does too.

Russ

peterGo

unread,
Sep 21, 2010, 10:04:14 AM9/21/10
to golang-nuts
Otan,

That's because they were using Microsoft's definition for the MS-DOS
and Windows file systems.

"Use a period to separate the base file name from the extension in the
name of a directory or file."
http://msdn.microsoft.com/en-us/library/aa365247.aspx#naming_conventions
Naming Files, Paths, and Namespaces
http://msdn.microsoft.com/en-us/library/aa365247.aspx

Other file systems and Go define a file name differently.

Peter
Reply all
Reply to author
Forward
0 new messages