FAQ
I'm looking for a way to embed a type who's constructor produces multiple
return values.

As a practical example, consider the fsnotify package's "NewWatcher" -->
http://godoc.org/gopkg.in/fsnotify.v1#NewWatcher

It returns a *Watcher along with a error

If I've declared my local struct as follows:

type localWatcher struct {
     *fsnotify.Watcher
}

How can I go about initializing localWatcher? The following does not work
as multiple values are returned by the NewWatcher constructor.

watcher:=localWatcher{fsnotify.NewWatcher()}

My current solution seems a bit verbose, so I'm wondering if there is a
more idiomatic Go way to accomplish the following:

_watcher,err := fsnotify.NewWatcher()
if err != nil {
     log.Fatal(err)
}
watcher := localWatcher{_watcher}

Thanks in advance.
-Chrys

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

  • Jesse McNelis at Aug 13, 2015 at 5:52 am

    On Thu, Aug 13, 2015 at 1:08 PM, wrote:
    My current solution seems a bit verbose, so I'm wondering if there is a more
    idiomatic Go way to accomplish the following:

    _watcher,err := fsnotify.NewWatcher()
    if err != nil {
    log.Fatal(err)
    }
    watcher := localWatcher{_watcher}
    This is the idiomatic Go way to accomplish this.
    If you get sick of typing it then make a function.

    --
    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.
  • Giulio Iotti at Aug 13, 2015 at 8:09 am

    On Thursday, August 13, 2015 at 7:52:56 AM UTC+2, Jesse McNelis wrote:
    On Thu, Aug 13, 2015 at 1:08 PM, <ch...@pcxchange.ca <javascript:>>
    wrote:
    My current solution seems a bit verbose, so I'm wondering if there is a more
    idiomatic Go way to accomplish the following:

    _watcher,err := fsnotify.NewWatcher()
    if err != nil {
    log.Fatal(err)
    }
    watcher := localWatcher{_watcher}
    This is the idiomatic Go way to accomplish this.
    If you get sick of typing it then make a function.

    Also worth reminding: the embedded watcher is accessible as "Watcher".

    w := localWatcher{} // Init other non-zero values here
    if w.Watcher, err = fsnotify.NewWatcher(); err != nil {
         log.Fatal(err)
    }

    --
    Giulio Iotti
    https://github.com/dullgiulio

    --
    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 13, '15 at 3:46a
activeAug 13, '15 at 8:09a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase