如何在程序退出时使用golang删除文件?
我已经创建了一个命令行应用程序,我在其中压缩文件夹并在本地服务器上共享以供其他人下载.我想要做的是在关闭服务器后立即删除压缩文件夹的副本.这是我的代码:
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","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:8080n",GetOutboundIP()) log.Fatal(http.ListenAndServe(":8080",nil)) }else{ fmt.Println("Invalid usage. No file mentioned. Use wshare -h for help.") } } 当我按下ctrl-c时,程序退出并且main函数关闭,因此,不应该执行os.Remove(xyz)吗? A tour of go说,defer在函数返回时执行表达式.在这里,我觉得主要没有得到任何回报的机会. 什么是解决方法来实现我想要做的事情?我有一些解决方案,比如等待按键等,但我希望这个程序非常简单,所以有没有办法在服务器关闭/程序退出时立即删除文件而不需要我的任何进一步输入? 解决方法
这已在评论中得到解答,但我会在此处记录完整性.
延迟仅在您正在使用它的程序和代码正常运行时才起作用.另一方面,使用命令停止程序或杀死程序会向程序发送信号,然后异常终止程序,这不允许程序干净地运行所有延迟语句. 如果要在OS终止时进行清理,可以监听OS信号 – 基于the example here的代码: sigs := make(chan os.Signal,1) signal.Notify(sigs,syscall.SIGINT,syscall.SIGTERM) go func() { <- sigs cleanupAllTheThings() os.Exit(0) }() 如果你从main调用它,它将保持goroutine运行程序的生命周期,听取操作系统信号.并且cleanupAllTheThings()函数需要被编写为尽可能快地运行而不会阻塞有效 – 你永远不知道操作系统何时会以偏见终止你. 此外,这不会保护您免受某人拔出插头或内核恐慌的影响 – 因此通常在启动时或在单独的清理脚本中对旧程序状态进行某种清理是有意义的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |