FAQ
Hi everyone I'm 47 minutes in to GO and running thru this example web
server provided by golang.org.

http://golang.org/doc/articles/wiki/

I was trying to understand the Function Literal and Closure and I clicked a
link trying to explain it in their wiki.

http://golang.org/ref/spec#Function_literals

The following snippet completely lost me, instead of just explaining a
Function Literal they added tons of complexity. Unless I need to know what
ACK is and the <- operator to understand Function Literals?

func(ch chan int) { ch <- ACK }(replyChan)

So it looks like we are making an anonymous function that accepts ch of
type chan, but then the int doesn't make sense. Maybe it's ch and chan of
type int? but there should be a comma in that case right?

Now ch <- ACK, What is the <- ? is ACK a go command for acknowledging the
handshake?

Finally the function is over after { } right, so whats the (replyChan)
telling us ? Is this a call back for when the function finishes?

Thanks so much !

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

  • Péter Szilágyi at Mar 11, 2014 at 3:00 pm
    Hi,

       If you have a little time, I would suggest the Go tour:
    http://tour.golang.org/. It covers almost all aspects of Go and is really
    the first stop for newcomers :). Btw, that is a channel (chan int together
    is the type), but you should really go through the tour :). It's simpler
    and much more thorough that anyone will take the time to answer here :P
    (not because of the question, but because it's already written too well not
    to use :) )

    Cheers,
       Peter

    On Tue, Mar 11, 2014 at 4:54 PM, Chuck Mongillo wrote:

    Hi everyone I'm 47 minutes in to GO and running thru this example web
    server provided by golang.org.

    http://golang.org/doc/articles/wiki/

    I was trying to understand the Function Literal and Closure and I clicked
    a link trying to explain it in their wiki.

    http://golang.org/ref/spec#Function_literals

    The following snippet completely lost me, instead of just explaining a
    Function Literal they added tons of complexity. Unless I need to know what
    ACK is and the <- operator to understand Function Literals?

    func(ch chan int) { ch <- ACK }(replyChan)

    So it looks like we are making an anonymous function that accepts ch of
    type chan, but then the int doesn't make sense. Maybe it's ch and chan of
    type int? but there should be a comma in that case right?

    Now ch <- ACK, What is the <- ? is ACK a go command for acknowledging the
    handshake?

    Finally the function is over after { } right, so whats the (replyChan)
    telling us ? Is this a call back for when the function finishes?

    Thanks so much !

    --
    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.
    --
    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.
  • Jan Mercl at Mar 11, 2014 at 3:01 pm

    On Tue, Mar 11, 2014 at 3:54 PM, Chuck Mongillo wrote:
    func(ch chan int) { ch <- ACK }(replyChan)

    So it looks like we are making an anonymous function that accepts ch of type
    chan, but then the int doesn't make sense. Maybe it's ch and chan of type
    int? but there should be a comma in that case right?
    The type of 'ch' is 'chan int': http://golang.org/ref/spec#Channel_types
    Now ch <- ACK, What is the <- ? is ACK a go command for acknowledging the
    handshake?
    ch <- expr is a send statement: http://golang.org/ref/spec#Send_statements
    Finally the function is over after { } right, so whats the (replyChan)
    telling us ? Is this a call back for when the function finishes?
    It's the argument to the function call, like in foo(42):
    http://golang.org/ref/spec#Calls

    I suggest to read the specs first and thanks to that ignore the tour
    later. YMMV.

    -j

    --
    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.
  • Chris dollin at Mar 11, 2014 at 3:03 pm

    On 11 March 2014 14:54, Chuck Mongillo wrote:

    Hi everyone I'm 47 minutes in to GO and running thru this example web
    server provided by golang.org.

    http://golang.org/doc/articles/wiki/

    I was trying to understand the Function Literal and Closure and I clicked
    a link trying to explain it in their wiki.

    http://golang.org/ref/spec#Function_literals

    The following snippet completely lost me, instead of just explaining a
    Function Literal they added tons of complexity.
    Yes, it does look a bit complexified.

    Unless I need to know what ACK is and the <- operator to understand
    Function Literals?

    func(ch chan int) { ch <- ACK }(replyChan)

    So it looks like we are making an anonymous function that accepts ch of
    type chan,
    The type is `chan int`. Channels (like slices and maps) are typed.
    A `chan int` can only accept and deliver ints.

    but then the int doesn't make sense. Maybe it's ch and chan of type int?
    but there should be a comma in that case right?

    Now ch <- ACK, What is the <- ?
    `channel <- value` sends the value down the channel.

    is ACK a go command for acknowledging the handshake?
    It's just an int variable or constant.
    Finally the function is over after { } right,
    So all that stuff on the left is the function [literal] and now we're
    applying it to some arguments . `replyChan` must be some `chan int`
    value.

    so whats the (replyChan) telling us ? Is this a call back for when the
    function finishes?
    It's where the function literal will send its ACK. Note the bit above that
    says

       A function literal can be assigned to a variable or invoked directly.

    This is the "invoked directly" bit.

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • Volker Dobler at Mar 11, 2014 at 3:05 pm
    Function closure are pretty well explained in the tour starting
    from page 44: http://tour.golang.org/#44. This and some inline
    comments below should make it clear.

    Am Dienstag, 11. März 2014 15:54:22 UTC+1 schrieb Chuck Mongillo:
    [...]
    The following snippet completely lost me, instead of just explaining a
    Function Literal they added tons of complexity. Unless I need to know what
    ACK is and the <- operator to understand Function Literals?
    No, <- channel receive and send have nothing to do with function or
    closures.

    func(ch chan int) { ch <- ACK }(replyChan)

    So it looks like we are making an anonymous function that accepts ch of
    type chan, but then the int doesn't make sense. Maybe it's ch and chan of
    type int? but there should be a comma in that case right?
    Anonymous function with one argument named ch which is of type "chan int".
    "chan int" is a channel of ints.

    Now ch <- ACK, What is the <- ? is ACK a go command for acknowledging the
    handshake?
    ch <- ACK sends ACK (which must be an int as ch is of type chan int) on ch.
    ACK is just something in this example.
    Finally the function is over after { } right, so whats the (replyChan)
    telling us ?
    Yes. And: (replyChan) invokes the anonymous function preceding it, just like
    calling a named function myFunc(replyChan) would be called.

    Is this a call back for when the function finishes?
    No, this is Go, not JavaScript :-)

    V.

    --
    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.
  • Chuck Mongillo at Mar 11, 2014 at 3:36 pm
    Thanks for the complete answers everyone, and yes I'll take the tour.
    Somehow I got on this web server guide first. :)

    --
    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
postedMar 11, '14 at 2:54p
activeMar 11, '14 at 3:36p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase