1

I have a function that plays a sound that I want to continue executing after my main program prints to standard output and exits. My reasoning is that I want the sound to finish playing after the program has exited but I don't want the main program to wait for the sound to finish playing before it exits.

I found a method for executing the sound in an independent process by turning it into an executable named playsound and doing go install. Then in my main program, I call this at the end of main():

func startPlaySound() {
    cmd := exec.Command("playsound")
    cmd.Start()
}

main() {
   // code that prints and exits

   startPlaySound()
}

This works but I would like to be able to play the sound after the main program exits without creating an executable file. I would rather run the equivalent of cmd.Start() on the function containing the code to play the sound.

In other words, I would like it to look something like this:

func playSound() {
    // code that plays the sound
}

func startPlaySound() {
   cmd := CmdFromFunction(playSound)
   cmd.Start()
}

main() {
   // code that prints and exits

   startPlaySound()
}

Does something like CmdFromFunction exist?

Response to Question Feedback

  • This is not a duplicate of How do I fork a go process?. I am not trying to fork the main goroutine. I am trying to detach playSound into a separate process that will continue executing after the main goroutine exits. Both of the answers to that question involve executing an external program, which is exactly what I am trying to avoid.
  • "go routines are not meant to be processes" - I literally turned the code from playSound into an executable called playsound and am now executing it as a process using cmd.Start(). I'm just trying to find a more direct route for doing that than creating an entirely separate executable file.
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
  • 3
    No, it doesn't. `exec` runs external executables, so whatever you want to run has to be an external executable. A function in your program can't be run after your program exits, by definition. – Adrian May 28 '19 at 19:49
  • 1
    "When that function invocation [main] returns, the program exits. It does not wait for other (non-main) goroutines to complete." https://golang.org/ref/spec#Program_execution – Peter May 28 '19 at 20:37
  • I understand that. My question is how to transform a goroutine into a full fledged process. It seems like a mechanism that should exist. – Chris Redford May 28 '19 at 20:55
  • go routines are not meant to be processes. – diyoda_ May 29 '19 at 06:20
  • Possible duplicate of [How do I fork a go process?](https://stackoverflow.com/questions/28370646/how-do-i-fork-a-go-process) – Biffen May 29 '19 at 06:52

1 Answers1

0

GO routines are not meant to be processes

This might work. I have done this myself, But you might be restricted to bash shell.

cmd := exec.Command("bash", "-c", "playsound", "&")
diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • Thanks for the answer but this is already accomplished by the first block of code in my question that calls `cmd.Start()`. What I want is to execute the code in `playsound` without having to have create a separate executable called `playsound` in the first place. – Chris Redford May 29 '19 at 13:11
  • Down vote and leave a comment why. I hope its not you @ChrisRedford – diyoda_ May 29 '19 at 20:57