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))
}
相关推荐
didiplus2 小时前
Python 入门第一课:为什么选择 Python?3 分钟搭建你的第一个程序
后端
dreamxian2 小时前
苍穹外卖day11
java·spring boot·后端·spring·mybatis
华科易迅3 小时前
Spring装配对象方法-注解
java·后端·spring
AwesomeDevin3 小时前
AI时代,我们的任务不应沉溺于与 AI 聊天,🤔 从“对话式编程”迈向“数字软件工厂”
前端·后端·架构
Victor3564 小时前
MongoDB(60)如何使用explain命令?
后端
Victor3564 小时前
MongoDB(59)如何分析查询性能?
后端
怒放吧德德7 小时前
Spring Boot实战:InfluxDB 2.x简单教程
java·spring boot·后端
后端不背锅7 小时前
可观测性体系:日志、指标、链路追踪
后端
苍何7 小时前
把小度音箱接入小龙虾是一种什么体验?
后端