FAQ
type Walker interface {
    Walk() error
}

type defaultWalker struct {}

func (d *defaultWalker) Walk() error {
   return nil
}

func NewWalker() *Walker {
   return &defaultWalker{}
}



The defaultWalker type is "private", no one should directly use it. To
continue that I thought NewWalker should return *Walker instead of
*defaultWalker, because I want clients to interact with a Walker (not
defaultWalker).

If I change it to this:

func NewWalker() *defaultWalker {
   return &defaultWalker{}
}



It "works", but seems like I might expose the defaultWalker type. Am I
missing something?

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

  • Scott Redig at Dec 17, 2014 at 5:50 pm
    NewWalker() should return type Walker (no pointer.)

    The reason being is that type *defaultWalker (pointer to default walker)
    satisfies the interface walker.
    On Wednesday, 17 December 2014 11:41:30 UTC-6, Craig Wickesser wrote:

    type Walker interface {
    Walk() error
    }

    type defaultWalker struct {}

    func (d *defaultWalker) Walk() error {
    return nil
    }

    func NewWalker() *Walker {
    return &defaultWalker{}
    }



    The defaultWalker type is "private", no one should directly use it. To
    continue that I thought NewWalker should return *Walker instead of
    *defaultWalker, because I want clients to interact with a Walker (not
    defaultWalker).

    If I change it to this:

    func NewWalker() *defaultWalker {
    return &defaultWalker{}
    }



    It "works", but seems like I might expose the defaultWalker type. Am I
    missing something?
    --
    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.
  • Craig Wickesser at Dec 17, 2014 at 6:09 pm
    So I tried:

    func NewWalker() Walker {
       return &defaultWalker{}
    }

    that fails with "cannot use defaultWalker literal (type *defaultWalker) as
    type Walker in return argument: *defaultWalker does not implement Walker"

    So I tried returning the instance instead of a pointer:

    func NewWalker() Walker {
       return defaultWalker{}
    }

    that fails with "cannot use defaultWalker literal (type defaultWalker) as
    type Walker in return argument: defaultWalker does not implement Walker"

    As shown, defaultWalker does have a Walk method.

    --
    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.
  • Craig Wickesser at Dec 17, 2014 at 6:10 pm
    actually it works, i changed the method signature :)
    On Wednesday, December 17, 2014 12:49:58 PM UTC-5, Scott Redig wrote:

    NewWalker() should return type Walker (no pointer.)

    The reason being is that type *defaultWalker (pointer to default walker)
    satisfies the interface walker.
    On Wednesday, 17 December 2014 11:41:30 UTC-6, Craig Wickesser wrote:

    type Walker interface {
    Walk() error
    }

    type defaultWalker struct {}

    func (d *defaultWalker) Walk() error {
    return nil
    }

    func NewWalker() *Walker {
    return &defaultWalker{}
    }



    The defaultWalker type is "private", no one should directly use it. To
    continue that I thought NewWalker should return *Walker instead of
    *defaultWalker, because I want clients to interact with a Walker (not
    defaultWalker).

    If I change it to this:

    func NewWalker() *defaultWalker {
    return &defaultWalker{}
    }



    It "works", but seems like I might expose the defaultWalker type. Am I
    missing something?
    --
    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
postedDec 17, '14 at 5:41p
activeDec 17, '14 at 6:10p
posts4
users2
websitegolang.org

2 users in discussion

Craig Wickesser: 3 posts Scott Redig: 1 post

People

Translate

site design / logo © 2021 Grokbase