FAQ
package a
func aaa (){
    print("a")
}

-------------------------------------------------------------------

import "a"
package main

func init(){
   a.aaa= func(){
     print("redefined")
   }
}

func main(){
   a.aaa()
}



but this is wrong
cannot assign to a.aaa,

How can I Override a.aaa ?


I want to do this.





--
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

  • David Symonds at Apr 10, 2014 at 8:43 am
    You can't unless you modify package a to make aaa a var.


    package a
    var aaa = func() { print("a") }

    --
    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.
  • Volker Dobler at Apr 10, 2014 at 8:46 am
    You cannot do this.
    First: a.aaa is not even exported. At least you should export it like func
    Aaa().
    Second: You cannot overwrite functions from other packages.

    But: Just expose the function as a variable:
    package a
    func a() { ... }
    var A = a

    package main
    a.A = func() { something else }
    a.A()

    V.

    Am Donnerstag, 10. April 2014 10:37:29 UTC+2 schrieb 小菜:
    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.




    --
    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.
  • Carlos Castillo at Apr 11, 2014 at 12:24 am
    If these answers aren't to your liking, perhaps you could tell us what you
    were trying to do that needed to arbitrarily replace functions at runtime.
    On Thursday, April 10, 2014 1:37:29 AM UTC-7, 小菜 wrote:

    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.




    --
    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.
  • 小菜 at Apr 11, 2014 at 2:34 am
    I kown this way, but is there any orther way?


    besause package a is a package on GitHub, I can't change the Existing code!




    在 2014年4月11日星期五UTC+8上午8时24分10秒,Carlos Castillo写道:
    If these answers aren't to your liking, perhaps you could tell us what you
    were trying to do that needed to arbitrarily replace functions at runtime.
    On Thursday, April 10, 2014 1:37:29 AM UTC-7, 小菜 wrote:

    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.




    --
    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.
  • Dan Kortschak at Apr 11, 2014 at 3:09 am
    If you provide the actual package and use case, then a more useful
    answer may be possible to provide.

    Without the context it is not possible to answer in a meaningful way.


    --
    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.
  • Egon at Apr 11, 2014 at 4:33 am

    On Friday, April 11, 2014 5:34:17 AM UTC+3, 小菜 wrote:
    I kown this way, but is there any orther way?
    You are not saying what is your actual problem. See
    https://code.google.com/p/go-wiki/wiki/HowToAsk

    besause package a is a package on GitHub, I can't change the Existing
    code!




    在 2014年4月11日星期五UTC+8上午8时24分10秒,Carlos Castillo写道:
    If these answers aren't to your liking, perhaps you could tell us what
    you were trying to do that needed to arbitrarily replace functions at
    runtime.
    On Thursday, April 10, 2014 1:37:29 AM UTC-7, 小菜 wrote:

    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.




    --
    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.
  • Volker Dobler at Apr 11, 2014 at 4:53 am

    Am Freitag, 11. April 2014 04:34:17 UTC+2 schrieb 小菜:
    I kown this way, but is there any orther way?
    No, there isn't.

    V.


    --
    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.
  • Tamás Gulácsi at Apr 11, 2014 at 6:34 am
    Fork the project, modify, and send a pull request. Till than, use your modified package.

    --
    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.
  • Michael Jones at Apr 11, 2014 at 6:38 am
    There is also embedding:
    http://golang.org/doc/effective_go.html#embedding

    On Fri, Apr 11, 2014 at 6:34 AM, Tamás Gulácsi wrote:

    Fork the project, modify, and send a pull request. Till than, use your
    modified package.

    --
    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.


    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765*

    --
    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.
  • Dan Kortschak at Apr 11, 2014 at 9:43 am
    That presumably won't work because the method (func field?) is not exported, so it's either internal or won't be satisfiable by a similarly named method defined in another package. But absence of context makes it difficult to say anything really useful.

    On 11/04/2014, at 4:08 PM, "Michael Jones" wrote:

    There is also embedding:
    http://golang.org/doc/effective_go.html#embedding

    --
    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.
  • Andrey Skoskin at Apr 11, 2014 at 12:07 pm
    If package "a" isn't very big, you can just write your own "proxy"-package
    with overriding everything you want and delegating rest.

    пятница, 11 апреля 2014 г., 6:34:17 UTC+4 пользователь 小菜 написал:
    I kown this way, but is there any orther way?


    besause package a is a package on GitHub, I can't change the Existing
    code!
    --
    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.
  • Chris dollin at Apr 11, 2014 at 12:12 pm

    On 10 April 2014 09:37, 小菜 wrote:

    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.
    You still haven't told us /why/ you want to do this. Odds
    are that the effect you're hoping to achieve can be achieved
    a different way (onc that doesn't risk breaking all the other
    things dependant on the meaning of the function you intend
    to replace).

    --
    Chris "allusive" Dollin

    --
    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.
  • 叶新伟 at Apr 11, 2014 at 2:56 pm
    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide the
    function.



    2014-04-11 20:12 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 10 April 2014 09:37, 小菜 wrote:

    package a
    func aaa (){
    print("a")
    }

    -------------------------------------------------------------------

    import "a"
    package main

    func init(){
    a.aaa= func(){
    print("redefined")
    }
    }

    func main(){
    a.aaa()
    }



    but this is wrong
    cannot assign to a.aaa,

    How can I Override a.aaa ?


    I want to do this.
    You still haven't told us /why/ you want to do this. Odds
    are that the effect you're hoping to achieve can be achieved
    a different way (onc that doesn't risk breaking all the other
    things dependant on the meaning of the function you intend
    to replace).

    --
    Chris "allusive" Dollin
    --
    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.
  • Chris dollin at Apr 11, 2014 at 3:02 pm

    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • 叶新伟 at Apr 11, 2014 at 3:42 pm
    I kown how to fix it, and I have do it.
    but I think, how to Simply overwride the function ?


    2014-04-11 23:02 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • 叶新伟 at Apr 11, 2014 at 3:44 pm
    I kown how to fix it, and I have done it.
    but I think, how to Simply overwride the function ?


    2014-04-11 23:02 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • 叶新伟 at Apr 11, 2014 at 3:44 pm
    I kown how to fix it, and I have do it.
    but I think, how to simply overwride the function ?


    2014-04-11 23:02 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • 叶新伟 at Apr 11, 2014 at 3:47 pm
    I kown how to fix it, and I have done it.
    but I think, how to simply overwride the function ?
    May be this is a useful features of Golang!


    2014-04-11 23:02 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • 叶新伟 at Apr 11, 2014 at 3:47 pm
    I kown how to fix it, and I have done it.
    but I think, how to simply overwride the function ?
    Maybe this is a useful features of Golang!


    2014-04-11 23:02 GMT+08:00 chris dollin <ehog.hedge@googlemail.com>:
    On 11 April 2014 15:55, 叶新伟 wrote:

    besause package a is a package on GitHub, I want't to change the code,
    because the package have beed import by Several go program。
    but the function in the package has lots of bugs, so I need to overwide
    the function.
    Have you contacted the maintainer of that package and told
    them about the bugs? Are you sure that they are really bugs?
    Or you can fork their package and fix the bugs and then you can
    use the fixed package -- and so can anyone else.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Andy Balholm at Apr 11, 2014 at 4:22 pm

    On Friday, April 11, 2014 8:47:10 AM UTC-7, 小菜 wrote:
    I kown how to fix it, and I have done it.
    but I think, how to simply overwride the function ?
    Maybe this is a useful features of Golang!
    No, the fact that you can't simply override the function is a useful
    feature of Go. When you import a package, you know what you're getting. It
    can't be changed out from under you.

    That is one of the major differences between static languages like Go and
    dynamic languages like Ruby. Most people think about the type system when
    they look at that distinction, but it usually affects other aspects of the
    language as well. Each style of language has its advantages and
    disadvantages, but little would be gained by trying to turn Go into Ruby.
    If you want to use Ruby, you'll need to use Ruby.

    --
    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.
  • Francesc Campoy Flores at Apr 11, 2014 at 4:43 pm
    Hi Chris,

    You might want to read this document explaining how to contribute to a
    project on GitHub.

    https://help.github.com/articles/fork-a-repo

    Good luck,

    On Fri, Apr 11, 2014 at 6:22 PM, Andy Balholm wrote:


    On Friday, April 11, 2014 8:47:10 AM UTC-7, 小菜 wrote:

    I kown how to fix it, and I have done it.
    but I think, how to simply overwride the function ?
    Maybe this is a useful features of Golang!
    No, the fact that you can't simply override the function is a useful
    feature of Go. When you import a package, you know what you're getting. It
    can't be changed out from under you.

    That is one of the major differences between static languages like Go and
    dynamic languages like Ruby. Most people think about the type system when
    they look at that distinction, but it usually affects other aspects of the
    language as well. Each style of language has its advantages and
    disadvantages, but little would be gained by trying to turn Go into Ruby.
    If you want to use Ruby, you'll need to use Ruby.

    --
    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.


    --
    --
    Francesc Campoy
    http://twitter.com/francesc

    --
    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.
  • Francesc Campoy Flores at Apr 11, 2014 at 6:00 pm
    And by Chris I meant 叶新伟!

    The gmail UI made me think Chris was the original poster, sorry.


    On Fri, Apr 11, 2014 at 6:43 PM, Francesc Campoy Flores
    wrote:
    Hi Chris,

    You might want to read this document explaining how to contribute to a
    project on GitHub.

    https://help.github.com/articles/fork-a-repo

    Good luck,

    On Fri, Apr 11, 2014 at 6:22 PM, Andy Balholm wrote:


    On Friday, April 11, 2014 8:47:10 AM UTC-7, 小菜 wrote:

    I kown how to fix it, and I have done it.
    but I think, how to simply overwride the function ?
    Maybe this is a useful features of Golang!
    No, the fact that you can't simply override the function is a useful
    feature of Go. When you import a package, you know what you're getting. It
    can't be changed out from under you.

    That is one of the major differences between static languages like Go and
    dynamic languages like Ruby. Most people think about the type system when
    they look at that distinction, but it usually affects other aspects of the
    language as well. Each style of language has its advantages and
    disadvantages, but little would be gained by trying to turn Go into Ruby.
    If you want to use Ruby, you'll need to use Ruby.

    --
    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.


    --
    --
    Francesc Campoy
    http://twitter.com/francesc


    --
    --
    Francesc Campoy
    http://twitter.com/francesc

    --
    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
postedApr 10, '14 at 8:37a
activeApr 11, '14 at 6:00p
posts23
users12
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase