Execute a go method in another process where the main process might exit out, but child process should complete execution.
I was going through goroutines where I can execute methods concurrently, but the method stops execution once the main process exits.
func f() {
time.Sleep(15 * time.Second)
d1 := []byte("hello\ngo\n")
err := ioutil.WriteFile("test", d1, 0644)
if err != nil {
fmt.Println(err.Error())
}
file, err := os.Create("test")
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
d2 := []byte{115, 111, 109, 101, 10}
n2, err := file.Write(d2)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("wrote %d bytes\n", n2)
}
func main() {
go f()
fmt.Println("done")
}
In the above function F, there is a sleep of 15 seconds. I want my main to exit out but my function f should run in background and finish the file creation. Is there any way we can achieve that without using os.exec().