'Do goroutines keep running even if main function is terminated?

I am running a server where main fires off several go routines. like this:

main() {
     go someFn(){
       // will run for infinite time()
     }
     go otherFn()
}

I have two doubts:

  1. what if the main function is exited? will these threads will still run or will terminate with the main function?

  2. if no, then what is the best method to make the main function run forever/ or run till I need it? currently, I am using select{} command for making it run forever! is there any better and more efficient method available than select{}



Solution 1:[1]

I would recommend to read the language specification in its entirety — Go is one of a very small number of laguages whose language spec can really be read over two lunches (or a single one — if this is your, I dunno, third of fourth programming language).

To cite it:

Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.


I would add that logically making main wait all non-main goroutines would be a bad thing for two reasons:

  1. That would easily create situations where you could wait for a program to shut down for some indeterminate amount of time even if you do not want that (and you not always want that).

    Logically, that would require inventing a "special" way to end main — something like abort(3) from C, and having such a way is a sure path for it to be abused.

  2. Any sensible usage of goroutines where you actually need to wait on them anyway requires explicit synchronisation — just per the language specification and its memory model.
    That is, when you want to wait on outstanding goroutines, you must do that explicitly.

Solution 2:[2]

[Do] go routines keeps on running even if main function is terminated?

No.

This is asked once a week.

Solution 3:[3]

In my opinion, when it is difficult to remember a concept the best thing is to code such concept yourself.

Here is a simple program which demonstrates that go routines are terminated when main function exits:

package main

import (
    "log"
    "os"
    "time"
)

func main() {
    f, err := os.Create("./test.txt")
    if err != nil {
        log.Fatal("can not write to a file", err)
    }
    go func() {
        for {
            f.WriteString("new line created at: " + time.Now().String() + "\n")
            time.Sleep(1 * time.Second)
        }
    }()
    time.Sleep(5 * time.Second)
    // writing to the file by goroutine will be finished in 5 seconds
    // when main exits
}

Solution 4:[4]

Context is the best way, Context is designed to share data that can be considered “global” for a tree of React components.

https://reactjs.org/docs/context.html#when-to-use-context

Solution 5:[5]

It depends.

IMO, if it's static/constant I think your way is doing fine. In fact you can create a separate file when it's becoming bigger.

But if it's mutable, use useContext.

And if it's sensitive credentials like API keys or you want the data to be different for development and staging, you could write it in .env file. Adding ENV in React

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Volker
Solution 3
Solution 4 Ganesh B. Kadam
Solution 5