Is the main function a goroutine?











up vote
0
down vote

favorite
1












Is the main() function a goroutine? For example, I've seen a crash stack trace like the below, which makes me ask:



goroutine 1 [running]: main.binarySearch(0x0, 0x61, 0x43,
0xc420043e70, 0x19, 0x19, 0x10)
/home/---/go/src/github.com/----/sumnum.go:22 +0x80 main.main()
/home/---/go/src/github.com/---/sumnum.go:13 +0xc1 exit status 2









share|improve this question
























  • @Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
    – Manjeet Thakur
    Nov 20 at 8:46















up vote
0
down vote

favorite
1












Is the main() function a goroutine? For example, I've seen a crash stack trace like the below, which makes me ask:



goroutine 1 [running]: main.binarySearch(0x0, 0x61, 0x43,
0xc420043e70, 0x19, 0x19, 0x10)
/home/---/go/src/github.com/----/sumnum.go:22 +0x80 main.main()
/home/---/go/src/github.com/---/sumnum.go:13 +0xc1 exit status 2









share|improve this question
























  • @Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
    – Manjeet Thakur
    Nov 20 at 8:46













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Is the main() function a goroutine? For example, I've seen a crash stack trace like the below, which makes me ask:



goroutine 1 [running]: main.binarySearch(0x0, 0x61, 0x43,
0xc420043e70, 0x19, 0x19, 0x10)
/home/---/go/src/github.com/----/sumnum.go:22 +0x80 main.main()
/home/---/go/src/github.com/---/sumnum.go:13 +0xc1 exit status 2









share|improve this question















Is the main() function a goroutine? For example, I've seen a crash stack trace like the below, which makes me ask:



goroutine 1 [running]: main.binarySearch(0x0, 0x61, 0x43,
0xc420043e70, 0x19, 0x19, 0x10)
/home/---/go/src/github.com/----/sumnum.go:22 +0x80 main.main()
/home/---/go/src/github.com/---/sumnum.go:13 +0xc1 exit status 2






go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 9:23









Volker

19.7k24757




19.7k24757










asked Nov 20 at 7:28









Manjeet Thakur

744523




744523












  • @Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
    – Manjeet Thakur
    Nov 20 at 8:46


















  • @Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
    – Manjeet Thakur
    Nov 20 at 8:46
















@Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
– Manjeet Thakur
Nov 20 at 8:46




@Volker Well I'm trying to learn a golang and by mistake this error is generated and I see in the error panic: runtime error: index out of range so then this question comes in my mind
– Manjeet Thakur
Nov 20 at 8:46












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Yes, the main function runs in a goroutine (the main one).



According to https://tour.golang.org/concurrency/1




A goroutine is a lightweight thread managed by the Go runtime.
go f(x, y, z)
starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.




So according to this official document the main runs in the current goroutine.





Now let's have some fun with main and run this (So here the current goroutine runs the new goroutine) so here we have more than one goroutine which execute main() again! (Note: Access to shared memory must be synchronized):



package main

import (
"fmt"
"time"
)

var i = 3

func main() {
if i <= 0 {
return
}
i--
fmt.Println("Hi")
go main()
time.Sleep(100 * time.Millisecond)
}


output:



Hi
Hi
Hi




Let's calculate factorial using main() (one goroutine - no synchronization needed):



package main

import "fmt"

func main() {
if f <= 0 {
fmt.Println(acc)
return
}
acc *= f
f--
main()
}

var f = 5
var acc = 1


output:



120


Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).






share|improve this answer























  • Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
    – Flimzy
    Nov 27 at 15:15










  • @Flimzy: Hi, Thanks again for all your comments and support.+1
    – A.R
    Nov 27 at 18:29




















up vote
5
down vote














Is the main function a goroutine?




No.



The main function is a function.



In contrast,




A goroutine is a lightweight thread of execution. (source).




So goroutines execute functions, but goroutines are not functions, and there is not a 1-to-1 relationship between goroutines and functions.



However...



The main() function is executed in the first (and at startup, only) goroutine, goroutine #1.



But as soon as that function calls another function, then the main goroutine is no longer executing the main function, and is instead executing some other function.



So it's clear that a goroutine and a function are entirely different entities.



Do not conflate goroutines with functions!!



Functions and goroutines are entirely different concepts. And thinking of them as the same thing will lead to countless of confusion and problems.






share|improve this answer























    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',
    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%2f53388154%2fis-the-main-function-a-goroutine%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Yes, the main function runs in a goroutine (the main one).



    According to https://tour.golang.org/concurrency/1




    A goroutine is a lightweight thread managed by the Go runtime.
    go f(x, y, z)
    starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

    Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.




    So according to this official document the main runs in the current goroutine.





    Now let's have some fun with main and run this (So here the current goroutine runs the new goroutine) so here we have more than one goroutine which execute main() again! (Note: Access to shared memory must be synchronized):



    package main

    import (
    "fmt"
    "time"
    )

    var i = 3

    func main() {
    if i <= 0 {
    return
    }
    i--
    fmt.Println("Hi")
    go main()
    time.Sleep(100 * time.Millisecond)
    }


    output:



    Hi
    Hi
    Hi




    Let's calculate factorial using main() (one goroutine - no synchronization needed):



    package main

    import "fmt"

    func main() {
    if f <= 0 {
    fmt.Println(acc)
    return
    }
    acc *= f
    f--
    main()
    }

    var f = 5
    var acc = 1


    output:



    120


    Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).






    share|improve this answer























    • Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
      – Flimzy
      Nov 27 at 15:15










    • @Flimzy: Hi, Thanks again for all your comments and support.+1
      – A.R
      Nov 27 at 18:29

















    up vote
    3
    down vote



    accepted










    Yes, the main function runs in a goroutine (the main one).



    According to https://tour.golang.org/concurrency/1




    A goroutine is a lightweight thread managed by the Go runtime.
    go f(x, y, z)
    starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

    Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.




    So according to this official document the main runs in the current goroutine.





    Now let's have some fun with main and run this (So here the current goroutine runs the new goroutine) so here we have more than one goroutine which execute main() again! (Note: Access to shared memory must be synchronized):



    package main

    import (
    "fmt"
    "time"
    )

    var i = 3

    func main() {
    if i <= 0 {
    return
    }
    i--
    fmt.Println("Hi")
    go main()
    time.Sleep(100 * time.Millisecond)
    }


    output:



    Hi
    Hi
    Hi




    Let's calculate factorial using main() (one goroutine - no synchronization needed):



    package main

    import "fmt"

    func main() {
    if f <= 0 {
    fmt.Println(acc)
    return
    }
    acc *= f
    f--
    main()
    }

    var f = 5
    var acc = 1


    output:



    120


    Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).






    share|improve this answer























    • Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
      – Flimzy
      Nov 27 at 15:15










    • @Flimzy: Hi, Thanks again for all your comments and support.+1
      – A.R
      Nov 27 at 18:29















    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    Yes, the main function runs in a goroutine (the main one).



    According to https://tour.golang.org/concurrency/1




    A goroutine is a lightweight thread managed by the Go runtime.
    go f(x, y, z)
    starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

    Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.




    So according to this official document the main runs in the current goroutine.





    Now let's have some fun with main and run this (So here the current goroutine runs the new goroutine) so here we have more than one goroutine which execute main() again! (Note: Access to shared memory must be synchronized):



    package main

    import (
    "fmt"
    "time"
    )

    var i = 3

    func main() {
    if i <= 0 {
    return
    }
    i--
    fmt.Println("Hi")
    go main()
    time.Sleep(100 * time.Millisecond)
    }


    output:



    Hi
    Hi
    Hi




    Let's calculate factorial using main() (one goroutine - no synchronization needed):



    package main

    import "fmt"

    func main() {
    if f <= 0 {
    fmt.Println(acc)
    return
    }
    acc *= f
    f--
    main()
    }

    var f = 5
    var acc = 1


    output:



    120


    Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).






    share|improve this answer














    Yes, the main function runs in a goroutine (the main one).



    According to https://tour.golang.org/concurrency/1




    A goroutine is a lightweight thread managed by the Go runtime.
    go f(x, y, z)
    starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

    Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.




    So according to this official document the main runs in the current goroutine.





    Now let's have some fun with main and run this (So here the current goroutine runs the new goroutine) so here we have more than one goroutine which execute main() again! (Note: Access to shared memory must be synchronized):



    package main

    import (
    "fmt"
    "time"
    )

    var i = 3

    func main() {
    if i <= 0 {
    return
    }
    i--
    fmt.Println("Hi")
    go main()
    time.Sleep(100 * time.Millisecond)
    }


    output:



    Hi
    Hi
    Hi




    Let's calculate factorial using main() (one goroutine - no synchronization needed):



    package main

    import "fmt"

    func main() {
    if f <= 0 {
    fmt.Println(acc)
    return
    }
    acc *= f
    f--
    main()
    }

    var f = 5
    var acc = 1


    output:



    120


    Note: The codes above are just for clearly showing my viewpoints and is not good for production use (Using global variables should not be the first choice).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 15:37

























    answered Nov 20 at 7:41









    A.R

    1,9032919




    1,9032919












    • Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
      – Flimzy
      Nov 27 at 15:15










    • @Flimzy: Hi, Thanks again for all your comments and support.+1
      – A.R
      Nov 27 at 18:29




















    • Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
      – Flimzy
      Nov 27 at 15:15










    • @Flimzy: Hi, Thanks again for all your comments and support.+1
      – A.R
      Nov 27 at 18:29


















    Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
    – Flimzy
    Nov 27 at 15:15




    Thanks for updating your answer to no longer conflate functions and goroutines. I have removed my downvote and replaced it with an upvote.
    – Flimzy
    Nov 27 at 15:15












    @Flimzy: Hi, Thanks again for all your comments and support.+1
    – A.R
    Nov 27 at 18:29






    @Flimzy: Hi, Thanks again for all your comments and support.+1
    – A.R
    Nov 27 at 18:29














    up vote
    5
    down vote














    Is the main function a goroutine?




    No.



    The main function is a function.



    In contrast,




    A goroutine is a lightweight thread of execution. (source).




    So goroutines execute functions, but goroutines are not functions, and there is not a 1-to-1 relationship between goroutines and functions.



    However...



    The main() function is executed in the first (and at startup, only) goroutine, goroutine #1.



    But as soon as that function calls another function, then the main goroutine is no longer executing the main function, and is instead executing some other function.



    So it's clear that a goroutine and a function are entirely different entities.



    Do not conflate goroutines with functions!!



    Functions and goroutines are entirely different concepts. And thinking of them as the same thing will lead to countless of confusion and problems.






    share|improve this answer



























      up vote
      5
      down vote














      Is the main function a goroutine?




      No.



      The main function is a function.



      In contrast,




      A goroutine is a lightweight thread of execution. (source).




      So goroutines execute functions, but goroutines are not functions, and there is not a 1-to-1 relationship between goroutines and functions.



      However...



      The main() function is executed in the first (and at startup, only) goroutine, goroutine #1.



      But as soon as that function calls another function, then the main goroutine is no longer executing the main function, and is instead executing some other function.



      So it's clear that a goroutine and a function are entirely different entities.



      Do not conflate goroutines with functions!!



      Functions and goroutines are entirely different concepts. And thinking of them as the same thing will lead to countless of confusion and problems.






      share|improve this answer

























        up vote
        5
        down vote










        up vote
        5
        down vote










        Is the main function a goroutine?




        No.



        The main function is a function.



        In contrast,




        A goroutine is a lightweight thread of execution. (source).




        So goroutines execute functions, but goroutines are not functions, and there is not a 1-to-1 relationship between goroutines and functions.



        However...



        The main() function is executed in the first (and at startup, only) goroutine, goroutine #1.



        But as soon as that function calls another function, then the main goroutine is no longer executing the main function, and is instead executing some other function.



        So it's clear that a goroutine and a function are entirely different entities.



        Do not conflate goroutines with functions!!



        Functions and goroutines are entirely different concepts. And thinking of them as the same thing will lead to countless of confusion and problems.






        share|improve this answer















        Is the main function a goroutine?




        No.



        The main function is a function.



        In contrast,




        A goroutine is a lightweight thread of execution. (source).




        So goroutines execute functions, but goroutines are not functions, and there is not a 1-to-1 relationship between goroutines and functions.



        However...



        The main() function is executed in the first (and at startup, only) goroutine, goroutine #1.



        But as soon as that function calls another function, then the main goroutine is no longer executing the main function, and is instead executing some other function.



        So it's clear that a goroutine and a function are entirely different entities.



        Do not conflate goroutines with functions!!



        Functions and goroutines are entirely different concepts. And thinking of them as the same thing will lead to countless of confusion and problems.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 9:03

























        answered Nov 20 at 8:46









        Flimzy

        36.8k96496




        36.8k96496






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53388154%2fis-the-main-function-a-goroutine%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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'