goroutines in init functions

1,540 views
Skip to first unread message

Thomas Bushnell, BSG

unread,
Jun 20, 2012, 6:14:05 PM6/20/12
to golang-nuts
I just finished Rob Pike's (very nice) talk on lexers in go, in which he implies that an init routine cannot reliably depend on starting a goroutine and having it run to completion, but that seems inconsistent with this statement in the language specification:

"Package initialization—variable initialization and the invocation of init functions—happens in a single goroutine, sequentially, one package at a time. An init function may launch other goroutines, which can run concurrently with the initialization code. However, initialization always sequences the init functions: it will not start the next init until the previous one has returned."

Did this get changed after the talk, or am I misunderstanding something?

Thomas

zeebo

unread,
Jun 20, 2012, 6:15:59 PM6/20/12
to golan...@googlegroups.com
Yes, at the time of the talk he was accurate. This was changed with Go 1.

Vanja Pejovic

unread,
Jun 20, 2012, 6:17:43 PM6/20/12
to Thomas Bushnell, BSG, golang-nuts
This was changed sometime after the talk. Not direct proof, but check this

Dave Cheney

unread,
Jun 20, 2012, 6:18:41 PM6/20/12
to Thomas Bushnell, BSG, golang-nuts
Yes, somewhere between Rob's talk and Go1, go routines were allowed to run during the init phase.

The workaround has been removed now, https://code.google.com/p/go/source/detail?r=0b9ae2971811

Cheers

Dave

Aaron France

unread,
Jun 21, 2012, 2:43:38 PM6/21/12
to golang-nuts
Hi,

Looking through Go code and using gofmt I can see that the name for the
method receiver isn't standardized. Why is this? It seems that
regulating or at least having a standard 'accepted' name for the method
receiver would be beneficial to both the readability of code and the
style of Go overall.

If it's not idiomatic, I could maybe understand but it seems that this
would be beneficial rather than negative.

Regards,
Aaron

David Symonds

unread,
Jun 21, 2012, 5:09:12 PM6/21/12
to Aaron France, golang-nuts
On Thu, Jun 21, 2012 at 11:43 AM, Aaron France
<a.france.m...@gmail.com> wrote:

> Looking through Go code and using gofmt I can see that the name for the
> method receiver isn't standardized. Why is this? It seems that regulating or
> at least having a standard 'accepted' name for the method receiver would be
> beneficial to both the readability of code and the style of Go overall.

One could say a similar thing about function/method arguments. The
receiver is not very special at all; it's just syntactic sugar.

x.Foo(1, 2, 3) on func (x *X) Foo(a, b, c int)

is equivalent to

Foo(x, 1, 2, 3) on func Foo(x *x, a, b, c int)



Dave.

Aaron France

unread,
Jun 21, 2012, 5:17:52 PM6/21/12
to David Symonds, golang-nuts
Hi Dave,

The way I see it is like this:

The method receiver argument should say something about the method
that's receiving it. So something like self, this or similar go to great
lengths to show that the object being used is the object which the
method is tied to. Whereas function arguments are objects defined only
in the scope of that function, thus mean something only in the scope of
that function and therefore should have names which coincide with what
they are/are doing.

This is a real nitpicky thing to point out but as a Go learner I can
lose track easily of the name of the method since each method function
usually has something completely different. I vote for "self".

I understand that it's syntactic sugar. However, would people kill me if
I just used "self" in all my method functions?

Regards,
Aaron

John Asmuth

unread,
Jun 21, 2012, 5:24:38 PM6/21/12
to golan...@googlegroups.com, David Symonds
I used to use "me" a lot for receiver variables. Then I stopped caring and did what everyone else does, which is to use the first letter or two of the type.

David Symonds

unread,
Jun 21, 2012, 5:24:59 PM6/21/12
to Aaron France, golang-nuts
On Thu, Jun 21, 2012 at 2:17 PM, Aaron France
<a.france.m...@gmail.com> wrote:

> The method receiver argument should say something about the method that's
> receiving it. So something like self, this or similar go to great lengths to
> show that the object being used is the object which the method is tied to.
> Whereas function arguments are objects defined only in the scope of that
> function, thus mean something only in the scope of that function and
> therefore should have names which coincide with what they are/are doing.

I think you're overthinking the role of the method receiver.

> This is a real nitpicky thing to point out but as a Go learner I can lose
> track easily of the name of the method since each method function usually
> has something completely different. I vote for "self".
>
> I understand that it's syntactic sugar. However, would people kill me if I
> just used "self" in all my method functions?

It's fine if you also name your method arguments "arg0", "arg1", "arg2", etc.

Note this: the method callers never name the receiver; they name the
variable (or expression) they have. So the name of the method receiver
only matters to the code of the method. Given that, "self" is not a
useful name, especially if you end up passing that variable on to
something else that might be a method on a different type, or a
standalone function.


Dave.

Aaron France

unread,
Jun 21, 2012, 5:24:35 PM6/21/12
to John Asmuth, golan...@googlegroups.com
Aha! So there is a convention. I asked in #go-nuts (great channel) and there wasn't much of a consensus. If the first two letters of the type is the consensus, then I'll do that.

Anyone else? Do you also use the first two letters?

Jim Whitehead II

unread,
Jun 21, 2012, 5:29:00 PM6/21/12
to Aaron France, John Asmuth, golan...@googlegroups.com
On Thu, Jun 21, 2012 at 10:24 PM, Aaron France
<a.france.m...@gmail.com> wrote:
> Aha! So there is a convention. I asked in #go-nuts (great channel) and there
> wasn't much of a consensus. If the first two letters of the type is the
> consensus, then I'll do that.
>
> Anyone else? Do you also use the first two letters?

I used a shortened form of the type name. One letter, two letters,
whatever makes sense as I'm writing it.

David Symonds

unread,
Jun 21, 2012, 5:31:15 PM6/21/12
to Aaron France, John Asmuth, golan...@googlegroups.com
On Thu, Jun 21, 2012 at 2:24 PM, Aaron France
<a.france.m...@gmail.com> wrote:

> Aha! So there is a convention. I asked in #go-nuts (great channel) and there
> wasn't much of a consensus. If the first two letters of the type is the
> consensus, then I'll do that.
>
> Anyone else? Do you also use the first two letters?

The first one, two or three letters of the type name is pretty common.
It's a judgement call, as is much of programming style.


Dave.

Tonic Artos

unread,
Jun 21, 2012, 9:55:01 PM6/21/12
to Aaron France, John Asmuth, golang-nuts


On Jun 22, 2012 9:26 AM, "Aaron France" <a.france.m...@gmail.com> wrote:
>
> Aha! So there is a convention. I asked in #go-nuts (great channel) and there wasn't much of a consensus. If the first two letters of the type is the consensus, then I'll do that.
>
> Anyone else? Do you also use the first two letters?

I just use 'a' for all receiver variables. It has the benefit of being quick to type and consistent for all methods. It also reads well in the method signature.

func (a thing) methodName(args...

roger peppe

unread,
Jun 22, 2012, 3:25:18 AM6/22/12
to Aaron France, John Asmuth, golan...@googlegroups.com
On 21 June 2012 22:24, Aaron France <a.france.m...@gmail.com> wrote:
> Aha! So there is a convention. I asked in #go-nuts (great channel) and there
> wasn't much of a consensus. If the first two letters of the type is the
> consensus, then I'll do that.
>
> Anyone else? Do you also use the first two letters?

I use exactly the same form that I would if the receiver was simply
the first argument to a function.

That means that if I have, say:

func (s *Something) Foo(se *SomethingElse) {
bletch(s, se, "foo")
}

then I can easily decide to refactor it to become a function:

func Foo(s *Something, se *SomethingElse)

or a method on another object:

func (se *SomethingElse) Foo(s *Something)

without changing any of the body of the code.
Reply all
Reply to author
Forward
0 new messages