FAQ
(Sory for my English)
I'm doing something like this:
I have an interface
type Hero interface {
Fly()
Fight()
}
And these struct:
type Bat struct {}
func (b *Bat) Fly() {}
type Man struct {}
func (m *Man) Fight() {}
type BatMan struct {
*Bat
*Man
}

so I can:
var h Hero = BatMan{}
OK, its normal, but then I try:

type BatMan2 struct {
Bat
Man
}
var batman *BatMan2

var h Hero = batman

Its ok too!
It confusing me, "*batman*" is a pointer to the *BatMan2*, but in *BatMan2*there is no pointer to
*Bat* and *Man*, so how can "*batman*" can Fly() and Fight()?

--
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/groups/opt_out.

Search Discussions

  • Rémy Oudompheng at Aug 15, 2013 at 8:58 am
    2013/8/15 Cao Nguyên (Sory for my English)
    I'm doing something like this:
    I have an interface
    type Hero interface {
    Fly()
    Fight()
    }
    And these struct:
    type Bat struct {}
    func (b *Bat) Fly() {}
    type Man struct {}
    func (m *Man) Fight() {}
    type BatMan struct {
    *Bat
    *Man
    }

    so I can:
    var h Hero = BatMan{}
    OK, its normal, but then I try:
    type BatMan2 struct {
    Bat
    Man
    }

    var batman *BatMan2

    var h Hero = batman
    Its ok too!
    It confusing me, "batman" is a pointer to the BatMan2, but in BatMan2 there
    is no pointer to Bat and Man, so how can "batman" can Fly() and Fight()?
    A pointer to BatMan2 naturally defines pointers to Bat and Man through
    &batman.Bat and &batman.Man.

    A value of type BatMan2 will not be a Hero.

    See the specification about method sets.

    Rémy.

    --
    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/groups/opt_out.
  • Kyle Lemons at Aug 16, 2013 at 12:31 am
    I've put together (and others have probably polished) a wiki page about
    method sets too:

    https://code.google.com/p/go-wiki/wiki/MethodSets


    On Thu, Aug 15, 2013 at 1:58 AM, Rémy Oudompheng
    wrote:
    2013/8/15 Cao Nguyên <nvcnvn1@gmail.com>:
    (Sory for my English)
    I'm doing something like this:
    I have an interface
    type Hero interface {
    Fly()
    Fight()
    }
    And these struct:
    type Bat struct {}
    func (b *Bat) Fly() {}
    type Man struct {}
    func (m *Man) Fight() {}
    type BatMan struct {
    *Bat
    *Man
    }

    so I can:
    var h Hero = BatMan{}
    OK, its normal, but then I try:
    type BatMan2 struct {
    Bat
    Man
    }

    var batman *BatMan2

    var h Hero = batman
    Its ok too!
    It confusing me, "batman" is a pointer to the BatMan2, but in BatMan2 there
    is no pointer to Bat and Man, so how can "batman" can Fly() and Fight()?
    A pointer to BatMan2 naturally defines pointers to Bat and Man through
    &batman.Bat and &batman.Man.

    A value of type BatMan2 will not be a Hero.

    See the specification about method sets.

    Rémy.

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Cao Nguyên at Aug 16, 2013 at 2:26 pm
    Thanks!!!!
    I LOVE THE EXAMPLES!

    Vào 07:31:29 UTC+7 Thứ sáu, ngày 16 tháng tám năm 2013, Kyle Lemons đã viết:
    I've put together (and others have probably polished) a wiki page about
    method sets too:

    https://code.google.com/p/go-wiki/wiki/MethodSets


    On Thu, Aug 15, 2013 at 1:58 AM, Rémy Oudompheng <remyoud...@gmail.com<javascript:>
    wrote:
    2013/8/15 Cao Nguyên <nvc...@gmail.com <javascript:>>:
    (Sory for my English)
    I'm doing something like this:
    I have an interface
    type Hero interface {
    Fly()
    Fight()
    }
    And these struct:
    type Bat struct {}
    func (b *Bat) Fly() {}
    type Man struct {}
    func (m *Man) Fight() {}
    type BatMan struct {
    *Bat
    *Man
    }

    so I can:
    var h Hero = BatMan{}
    OK, its normal, but then I try:
    type BatMan2 struct {
    Bat
    Man
    }

    var batman *BatMan2

    var h Hero = batman
    Its ok too!
    It confusing me, "batman" is a pointer to the BatMan2, but in BatMan2 there
    is no pointer to Bat and Man, so how can "batman" can Fly() and Fight()?
    A pointer to BatMan2 naturally defines pointers to Bat and Man through
    &batman.Bat and &batman.Man.

    A value of type BatMan2 will not be a Hero.

    See the specification about method sets.

    Rémy.

    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 15, '13 at 8:54a
activeAug 16, '13 at 2:26p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase