go 使用systray 实现托盘和程序退出

1.先 go get 安装 包

bash 复制代码
go get github.com/getlantern/systray

2.使用的代码

bash 复制代码
func main() {
	fmt.Println("开始")
	systray.Run(onReady, onExit)
}

func onReady() {
	systray.SetIcon(icon.Data)
	systray.SetTitle("Awesome App")
	systray.SetTooltip("Pretty awesome超级棒")
	mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
	fmt.Println("开始启动")
	// Sets the icon of a menu item. Only available on Mac and Windows.
	mQuit.SetIcon(icon.Data)
	//监听点击Quit按钮
	go func() {
		<-mQuit.ClickedCh
		systray.Quit()
	}()
}
//关闭对应的程序,这里的command只适用于windows
func onExit() {
	// clean up here
	fmt.Println("结束")
	command := exec.Command("taskkill", "/im", "server.exe", "/F")
	output, err := command.CombinedOutput()
	if err != nil {
		fmt.Println("output err:", err)
		return
	}
	fmt.Println("output:", string(output))
}
相关推荐
Asthenia041233 分钟前
为什么说MVCC无法彻底解决幻读的问题?
后端
Asthenia041234 分钟前
面试官问我:三级缓存可以解决循环依赖的问题,那两级缓存可以解决Spring的循环依赖问题么?是不是无法解决代理对象的问题?
后端
Asthenia041235 分钟前
面试复盘:使用 perf top 和火焰图分析程序 CPU 占用率过高
后端
Asthenia041236 分钟前
面试复盘:varchar vs char 以及 InnoDB 表大小的性能分析
后端
Asthenia041236 分钟前
面试问题解析:InnoDB中NULL值是如何记录和存储的?
后端
Asthenia04121 小时前
面试官问我:TCP发送到IP存在但端口不存在的报文会发生什么?
后端
Asthenia04121 小时前
HTTP 相比 TCP 的好处是什么?
后端
Asthenia04121 小时前
MySQL count(*) 哪个存储引擎更快?为什么 MyISAM 更快?
后端
Asthenia04121 小时前
面试官问我:UDP发送到IP存在但端口不存在的报文会发生什么?
后端
Asthenia04121 小时前
深入理解 TCP backlog 参数:意义、应用场景与 Netty 中的应用
后端