You "cannot define new methods on non-local type[s]," by design.
The best practice is to embed the non-local type into your own own
local type, and extend it. Type-aliasing (type MyFoo Foo) creates a
type that is (more-or-less) completely distinct from the original. I'm
not aware of a straightforward/best-practice way to use type
assertions to get around that.
You can only add functions to types in the package where they are defined.
So you can't extend OS stuff.
John
=:->
Just convert it:
MyFileInfo(fi)
<http://golang.org/doc/go_spec.html#Conversions>
Note that os.Stat() returns a *os.FileInfo, so it might be easier to
instead define MyFileInfo as
type MyFileInfo *os.FileInfo
.
This works, as long as you don't mind stripping all the methods off
os.FileInfo (you can always access them with a type conversion, but
that might be inconvenient). If you want to have all the methods on
one type, just use the embedding approach. Don't worry about having a
struct with only one field, the struct doesn't carry any overhead.
Then, you don't need to use a *MyFileInfo, since it's already
effectively a pointer. Just define methods on, and pass around,
MyFileInfo directly. I find the embedding cleaner to use (if you care
about the old methods), since it saves you most conversions, and on
the few occasions you *do* have to convert, it's not syntactically
heavier, it's actually syntactically lighter:
A type's methods are in the package that defines it.
--
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/groups/opt_out.