如何在程序退出时使用golang删除文件?

Kra*_*ash 8 go

我已经创建了一个命令行应用程序,我在其中压缩文件夹并在本地服务器上共享以供其他人下载.我想要做的是在关闭服务器后立即删除压缩文件夹的副本.这是我的代码:

func main() {
    //flag to specify whether we will be uploading folder or a single file
    zipped := flag.Bool("z",false,"Use for zipping folders and serving them as a single file on the server.(Deletes the zipped file once the server closes.)")
    save := flag.Bool("s",false,"Use with -z for saving the zipped files locally even after the server closes.")
    flag.Parse()


    if len(flag.Args())>0{

        if *zipped{
            fmt.Println("zipping...")
            flag.Args()[0]=ZipFile()

            if !(*save){
              //I expect this to remove the file when I hit ctrl+c on cmd  
              defer os.Remove(flag.Args()[0])   
                 }
        }
        http.HandleFunc("/",ShareFile)
        fmt.Printf("Sharing file on %s:8080\n",GetOutboundIP())

        log.Fatal(http.ListenAndServe(":8080",nil))
    }else{
        fmt.Println("Invalid usage. No file mentioned. Use wshare -h for help.")
    }

}
Run Code Online (Sandbox Code Playgroud)

当我按下ctrl-c时,程序退出并且main函数关闭,因此,不应该执行os.Remove(xyz)吗?游览说,延迟执行函数返回时的表达式.在这里,我觉得主要没有得到任何回报的机会.

什么是解决方法来实现我想要做的事情?我有一些解决方案,比如等待按键等,但我希望这个程序非常简单,所以有没有办法在服务器关闭/程序退出时立即删除文件而不需要我的任何进一步输入?

Sud*_*han 13

这已在评论中得到解答,但我会在此处记录完整性.

defer仅当您正在使用它的程序和代码正常运行时才有效.另一方面,使用命令停止程序或杀死程序会向程序发送信号,然后异常终止程序,这不允许程序defer干净地运行所有语句.

如果要在OS终止时清理,可以监听OS信号 - 基于以下示例的代码:

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
  <- sigs
  cleanupAllTheThings()
  os.Exit(0)
}()
Run Code Online (Sandbox Code Playgroud)

如果你打电话给main它,它将保持一个goroutine运行程序的生命周期,听取操作系统信号.并且cleanupAllTheThings()函数需要被编写为尽可能快地运行而不会阻塞才能生效 - 您永远不知道操作系统何时会以偏见终止您.

此外,这不会保护您免受某人拔出插头或内核恐慌的影响 - 因此通常在启动时或在单独的清理脚本中对旧程序状态进行某种清理是有意义的.

  • 只需提及-文件的实际删除将是os.Remove(path)** (2认同)