FAQ
I have in my package, in one file, an init() which initializes some
variables, and in another file another init() which needs these variables
to be initialized.

So, for ordering their executions, I set a global 'done' channel which the
first one sends to, and which the other receives to.

// first.go

var done chan struct{}
func init() {
// ... initialize vars ...
     go func() {
         for {
             done <- struct{}{}
         }
     }()
}

// second.go

func init() {
     go func() {
<-done
// ... do things with vars ...
     }
}

But the sending is never done. Why? Is that because when init() finishes
its goroutines get killed? I don't see that clear from the spec.

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

  • Toni Cárdenas at May 30, 2013 at 11:06 am
    Rémy Oudompheng solved the issue ('done' is nil) in private (by mistake, I
    guess).
    On Thursday, May 30, 2013 12:43:18 PM UTC+2, Toni Cárdenas wrote:

    I have in my package, in one file, an init() which initializes some
    variables, and in another file another init() which needs these variables
    to be initialized.

    So, for ordering their executions, I set a global 'done' channel which the
    first one sends to, and which the other receives to.

    // first.go

    var done chan struct{}
    func init() {
    // ... initialize vars ...
    go func() {
    for {
    done <- struct{}{}
    }
    }()
    }

    // second.go

    func init() {
    go func() {
    <-done
    // ... do things with vars ...
    }
    }

    But the sending is never done. Why? Is that because when init() finishes
    its goroutines get killed? I don't see that clear from the spec.
    --
    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.
  • Martin Schnabel at May 30, 2013 at 11:07 am

    On 05/30/2013 12:43 PM, Toni Cárdenas wrote:
    I have in my package, in one file, an init() which initializes some
    variables, and in another file another init() which needs these
    variables to be initialized.

    So, for ordering their executions, I set a global 'done' channel which
    the first one sends to, and which the other receives to.

    // first.go

    var done chan struct{}
    func init() {
    // ... initialize vars ...
    go func() {
    for {
    done <- struct{}{}
    }
    }()
    }

    // second.go

    func init() {
    go func() {
    <-done
    // ... do things with vars ...
    }
    }

    But the sending is never done. Why? Is that because when init() finishes
    its goroutines get killed? I don't see that clear from the spec.
    This works:
        http://play.golang.org/p/iOOJ5OkRAn

    I am not sure where the issue was. your code does not show the creation
    of the done channel and the main function may return before the go
    routines run.

    you may also look at http://golang.org/ref/spec#Program_execution for
    more details on program initialization and its dependency analysis.
    i think you can achieve the same order without channel and go routines:

        http://play.golang.org/p/Cm4BezvExM

    --
    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.
  • Daniel Morsing at May 30, 2013 at 11:10 am

    On Thu, May 30, 2013 at 12:43 PM, Toni Cárdenas wrote:
    I have in my package, in one file, an init() which initializes some
    variables, and in another file another init() which needs these variables to
    be initialized.

    So, for ordering their executions, I set a global 'done' channel which the
    first one sends to, and which the other receives to.

    // first.go

    var done chan struct{}
    func init() {
    // ... initialize vars ...
    go func() {
    for {
    done <- struct{}{}
    }
    }()
    }

    // second.go

    func init() {
    go func() {
    <-done
    // ... do things with vars ...
    }
    }

    But the sending is never done. Why? Is that because when init() finishes its
    goroutines get killed? I don't see that clear from the spec.
    This construction isn't necessary. Package level values are
    initialized in data dependent order and as such you can do this:

    var f = NewFoo()
    var b = NewBar()

    func NewBar() Bar {
         return f.GetBar()
    }

    and it should just work. The order of evaluation for init functions is
    unspecified.
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 30, '13 at 10:43a
activeMay 30, '13 at 11:10a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase