Golang Function Call Without Parentheses










-1















I am going through the Golang tutorials on their website and am confused by code similar to this that I've simplified and reproduced here:



package main

import (
"fmt"
"math"
)

func main()
a := math.Sqrt2
fmt.Println(a)



This prints 1.4142135623730951 in the sandbox. Replacing a := math.Sqrt2 with a := math.Sqrt(2) does the same thing but I'm confused how the function can be called without parentheses. math.Sqrt is not a function pointer here (there is no math.Sqrt2 function anyway, it's a function being passed without any parentheses. The function in the Go documentation here is listed as: func Sqrt(x float64) float64 i.e. with the parameter. So how does that work? Is it just because math.Sqrt() is a simplistic function that Go can assume it's a float64 without the parentheses passed? Am I missing something?



If it helps, I found this phenomenon here in the tutorials on line 14, originally. If anyone could explain this feature to me, that would be awesome. I'd love to learn about it.










share|improve this question

















  • 10





    math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

    – zerkms
    Nov 14 '18 at 22:15












  • Ahh, I see. Thanks!

    – Harpagornis
    Nov 15 '18 at 19:25















-1















I am going through the Golang tutorials on their website and am confused by code similar to this that I've simplified and reproduced here:



package main

import (
"fmt"
"math"
)

func main()
a := math.Sqrt2
fmt.Println(a)



This prints 1.4142135623730951 in the sandbox. Replacing a := math.Sqrt2 with a := math.Sqrt(2) does the same thing but I'm confused how the function can be called without parentheses. math.Sqrt is not a function pointer here (there is no math.Sqrt2 function anyway, it's a function being passed without any parentheses. The function in the Go documentation here is listed as: func Sqrt(x float64) float64 i.e. with the parameter. So how does that work? Is it just because math.Sqrt() is a simplistic function that Go can assume it's a float64 without the parentheses passed? Am I missing something?



If it helps, I found this phenomenon here in the tutorials on line 14, originally. If anyone could explain this feature to me, that would be awesome. I'd love to learn about it.










share|improve this question

















  • 10





    math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

    – zerkms
    Nov 14 '18 at 22:15












  • Ahh, I see. Thanks!

    – Harpagornis
    Nov 15 '18 at 19:25













-1












-1








-1








I am going through the Golang tutorials on their website and am confused by code similar to this that I've simplified and reproduced here:



package main

import (
"fmt"
"math"
)

func main()
a := math.Sqrt2
fmt.Println(a)



This prints 1.4142135623730951 in the sandbox. Replacing a := math.Sqrt2 with a := math.Sqrt(2) does the same thing but I'm confused how the function can be called without parentheses. math.Sqrt is not a function pointer here (there is no math.Sqrt2 function anyway, it's a function being passed without any parentheses. The function in the Go documentation here is listed as: func Sqrt(x float64) float64 i.e. with the parameter. So how does that work? Is it just because math.Sqrt() is a simplistic function that Go can assume it's a float64 without the parentheses passed? Am I missing something?



If it helps, I found this phenomenon here in the tutorials on line 14, originally. If anyone could explain this feature to me, that would be awesome. I'd love to learn about it.










share|improve this question














I am going through the Golang tutorials on their website and am confused by code similar to this that I've simplified and reproduced here:



package main

import (
"fmt"
"math"
)

func main()
a := math.Sqrt2
fmt.Println(a)



This prints 1.4142135623730951 in the sandbox. Replacing a := math.Sqrt2 with a := math.Sqrt(2) does the same thing but I'm confused how the function can be called without parentheses. math.Sqrt is not a function pointer here (there is no math.Sqrt2 function anyway, it's a function being passed without any parentheses. The function in the Go documentation here is listed as: func Sqrt(x float64) float64 i.e. with the parameter. So how does that work? Is it just because math.Sqrt() is a simplistic function that Go can assume it's a float64 without the parentheses passed? Am I missing something?



If it helps, I found this phenomenon here in the tutorials on line 14, originally. If anyone could explain this feature to me, that would be awesome. I'd love to learn about it.







function go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 22:14









HarpagornisHarpagornis

193




193







  • 10





    math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

    – zerkms
    Nov 14 '18 at 22:15












  • Ahh, I see. Thanks!

    – Harpagornis
    Nov 15 '18 at 19:25












  • 10





    math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

    – zerkms
    Nov 14 '18 at 22:15












  • Ahh, I see. Thanks!

    – Harpagornis
    Nov 15 '18 at 19:25







10




10





math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

– zerkms
Nov 14 '18 at 22:15






math.Sqrt2 is a constant golang.org/pkg/math/#pkg-constants golang.org/src/math/const.go

– zerkms
Nov 14 '18 at 22:15














Ahh, I see. Thanks!

– Harpagornis
Nov 15 '18 at 19:25





Ahh, I see. Thanks!

– Harpagornis
Nov 15 '18 at 19:25












1 Answer
1






active

oldest

votes


















6














There is nothing special happening here. math.Sqrt2 is a constant. You can find the other constants in the math package in the docs.



In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.






share|improve this answer























  • Got it! I was overthinking it! Thanks a lot!

    – Harpagornis
    Nov 15 '18 at 19:26











  • "go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

    – zerkms
    Nov 15 '18 at 20:06











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309537%2fgolang-function-call-without-parentheses%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














There is nothing special happening here. math.Sqrt2 is a constant. You can find the other constants in the math package in the docs.



In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.






share|improve this answer























  • Got it! I was overthinking it! Thanks a lot!

    – Harpagornis
    Nov 15 '18 at 19:26











  • "go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

    – zerkms
    Nov 15 '18 at 20:06
















6














There is nothing special happening here. math.Sqrt2 is a constant. You can find the other constants in the math package in the docs.



In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.






share|improve this answer























  • Got it! I was overthinking it! Thanks a lot!

    – Harpagornis
    Nov 15 '18 at 19:26











  • "go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

    – zerkms
    Nov 15 '18 at 20:06














6












6








6







There is nothing special happening here. math.Sqrt2 is a constant. You can find the other constants in the math package in the docs.



In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.






share|improve this answer













There is nothing special happening here. math.Sqrt2 is a constant. You can find the other constants in the math package in the docs.



In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 22:36









poypoy

6,50763364




6,50763364












  • Got it! I was overthinking it! Thanks a lot!

    – Harpagornis
    Nov 15 '18 at 19:26











  • "go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

    – zerkms
    Nov 15 '18 at 20:06


















  • Got it! I was overthinking it! Thanks a lot!

    – Harpagornis
    Nov 15 '18 at 19:26











  • "go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

    – zerkms
    Nov 15 '18 at 20:06

















Got it! I was overthinking it! Thanks a lot!

– Harpagornis
Nov 15 '18 at 19:26





Got it! I was overthinking it! Thanks a lot!

– Harpagornis
Nov 15 '18 at 19:26













"go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

– zerkms
Nov 15 '18 at 20:06






"go doesn't really have any "magic"" --- unless it does: expressions may behave differently depending on the context. Eg: as a part of a composite expression vs as a part of return (multiple value returns). To me - that's totally counter-intuitive magic

– zerkms
Nov 15 '18 at 20:06




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309537%2fgolang-function-call-without-parentheses%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3