FAQ
Why is it that a call to a no params function omitting the parentheses
returns a pointer to the func signature and not a *"...is not an
expression, must be called"* error message?

fmt.Printf("%v\t%T\n", os.Getgid, os.Getgid) // 0x426537 func() int
fmt.Printf("%v\t%T\n", os.Getgid(), os.Getgid()) // -1 int

--

Search Discussions

  • Jan Mercl at Oct 1, 2012 at 12:58 pm

    On Mon, Oct 1, 2012 at 2:50 PM, Dumitru Ungureanu wrote:
    Why is it that a call to a no params function omitting the parentheses
    returns a pointer
    b/c w/o the parenthesis it is not a call: http://golang.org/ref/spec#Calls.
    to the func signature
    to the function (not signature)
    and not a "...is not an expression,
    must be called" error message?
    b/c how otherwise would be `funcVar := funcExpr` possible? Ex: `print
    := fmt.Println; fmt.Println("foo"); print("bar")`

    -j

    --
  • Kevin Gillette at Oct 1, 2012 at 1:08 pm
    Because those aren't calls (parentheses can't be omitted in a call... this
    isn't perl/lua/whatever), they're pointer references to the functions. If
    you want to do anything "interesting" with functions, this is very
    important -- net/http wouldn't work if function references couldn't be
    valid expressions.

    Try this:

    x := os.Getgid
    fmt.Println(x())
    On Monday, October 1, 2012 6:50:21 AM UTC-6, Dumitru Ungureanu wrote:

    Why is it that a call to a no params function omitting the parentheses
    returns a pointer to the func signature and not a *"...is not an
    expression, must be called"* error message?

    fmt.Printf("%v\t%T\n", os.Getgid, os.Getgid) // 0x426537 func() int
    fmt.Printf("%v\t%T\n", os.Getgid(), os.Getgid()) // -1 int
    --
  • Dumitru Ungureanu at Oct 1, 2012 at 2:18 pm
    Ah, OK.

    So since function in Go are types, this declares a type:
    x := os.Getgid

    What happens when I attach parenthesis to the type?
    x()

    I mean, I know what happens (now), it's a function call, not a type
    declaration, but what's the mechanism?

    Thanks.

    --
  • Jan Mercl at Oct 1, 2012 at 2:26 pm

    On Mon, Oct 1, 2012 at 4:10 PM, Dumitru Ungureanu wrote:
    Ah, OK.

    So since function in Go are types, this declares a type:
    x := os.Getgid
    Declares a variable `x` which type is `func()` and set its value to
    os.Getgid (basically a memory address of that function entry code).
    What happens when I attach parenthesis to the type?
    x()
    If you now write `x()` the you call the function value stored in `x`
    == call the function x points to == call os.Getgid, so now `x()` ==
    `os.Getgid()`.
    I mean, I know what happens (now), it's a function call, not a type
    declaration, but what's the mechanism?
    It's not a type decl it is a fn call.

    -j

    --
  • Chris dollin at Oct 1, 2012 at 2:54 pm

    On 1 October 2012 15:10, Dumitru Ungureanu wrote:
    Ah, OK.

    So since function in Go are types,
    Go functions are not types. They /have/ types, just like all
    the other values in Go.
    this declares a type:
    x := os.Getgid
    No, it declares the variable x. It has whatever type os.Getgid has,
    which (consults godoc) is func()int.
    What happens when I attach parenthesis to the type?
    x()
    That's not what you're doing (since x isn't a type). You're
    writing a function call of whatever function x currently is
    (viz, os.Getgid) and no arguments. The result will be that
    os.Getgid is called (and its result is dropped on the floor.)
    I mean, I know what happens (now), it's a function call, not a type
    declaration, but what's the mechanism?
    The declaration copies the value of the function os.Getgid
    into x. "The value of the function" will be something very
    like a pointer to its code, or a structure that contains a pointer
    to its code. The call calls whatever function is pointed to.
    Since there are no closures involved, that's pretty much
    what happens in C or C++ pointer-to-functions, or in
    Scheme functions or Pop11 procedures: No New Magic.

    Chris

    --
    Chris "allusive" Dollin

    --
  • Dumitru Ungureanu at Oct 1, 2012 at 3:59 pm
    Yeah, variable, not type declaration, sorry, bad bad wording from my part.

    So, I declare a variable x, that Go sees it like this: (func()
    int)(0x428c87). From what I gather so far, 0x428c87 is the address where
    the code for os.Getgid is preped and loaded, waiting for execute calls like
    x()?

    x := os.Getegid would be like an import for the function Getegid?

    --
  • Dumitru Ungureanu at Oct 1, 2012 at 4:08 pm
    ... the start in the memory address, where the code is preped and loaded...

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 1, '12 at 12:50p
activeOct 1, '12 at 4:08p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase