FAQ
Hi,

why does golang not support the following?
Why can't I add a writer interface to, eg, an int
or to a type that I didn't define.
...I should be able to make another library's class usable as a writer.

func (v int32) Write(p []byte) {
   v = 42
}
prog.go:5: cannot define new methods on non-local type int32

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Aug 29, 2014 at 11:02 pm

    On Fri, Aug 29, 2014 at 4:00 PM, wrote:
    why does golang not support the following?
    Why can't I add a writer interface to, eg, an int
    or to a type that I didn't define.
    ...I should be able to make another library's class usable as a writer.

    func (v int32) Write(p []byte) {
    v = 42
    }
    prog.go:5: cannot define new methods on non-local type int32
    If you could add your own method to a type defined in some other
    package, then whether a type satisfies an interface becomes
    indeterminate. It depends on which package instantiated the specific
    value of the type, not on where the type is defined.

    Ian

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 29, '14 at 11:00p
activeAug 29, '14 at 11:02p
posts2
users2
websitegolang.org

2 users in discussion

Ian Lance Taylor: 1 post Gerald Stanje: 1 post

People

Translate

site design / logo © 2022 Grokbase