Go代码程序无法在后台运行

Go code running in the background

I am a beginner of the go language. I wrote a small program that made a keyboard sound. After go build main.go, you can hear the sound of the button in the current shell. But when running ./main in the background or when re-opening a new shell will not hear the button sound. This is what I need help with.

package main

import (
    "fmt"
    "github.com/eiannone/keyboard"
    "github.com/faiface/beep"
    "github.com/faiface/beep/speaker"
    "github.com/faiface/beep/wav"
    "os"
    "time"
    "log"
    "path/filepath"

)

func main(){

    env:= os.Getenv("HOME")
    fmt.Println(env)

    err := keyboard.Open()
    if err != nil {

        panic(err)
    }
    defer keyboard.Close()
    for {
        char, key, err := keyboard.GetKey()
        if err != nil {
            log.Fatal(err)
        }else if (key == keyboard.KeyEsc){
            break
        }

        n := int(char)
        path := "./wav/*.wav"
        fpath,_ := filepath.Glob(path)
        name := fpath[n]

        f, err := os.Open(name)
        if err != nil {
            log.Fatal(err)
        }

        streamer, format, err := wav.Decode(f)
        if err != nil {
            log.Fatal(err)
        }
        defer streamer.Close()

        speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

        done := make(chan bool)
        speaker.Play(beep.Seq(streamer, beep.Callback(func() {
            done <- true
        })))

        <-done

    }

}

I want it to run in the background. I can hear the corresponding sound after pressing the keyboard.

If the program is running in the background it will not get any input from the keyboard.

Also since n depends on user input it would be easy for it to exceed the number of sound files and you would get an index out of bounds error at run-time. You should do something like this:

    if fpath, _ := filepath.Glob("./wav/*.wav"); len(fpath) > 0 {
        n := int(char)%len(fpath)
           name := fpath[n]
        ...