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))
}
相关推荐
就是帅我不改23 分钟前
10万QPS压垮系统?老司机一招线程池优化,让性能飞起来!
后端·面试·github
uzong24 分钟前
系统稳定性保障:研发规约V1.0
后端
Ray6624 分钟前
log4j2.xml配置文件详解
后端
Frank_zhou27 分钟前
Easy-Es 架构设计详解
后端·elasticsearch
狗头大军之江苏分军37 分钟前
Meta万人裁员亲历者自述:小扎尝到了降本的甜头
前端·后端·github
Jagger_1 小时前
SonarQube:提升代码质量的前后端解决方案
前端·后端·ai编程
在逃牛马2 小时前
【Uni-App+SSM 宠物项目实战】Day6:MP 实体类与 Mapper 生成
后端
remaindertime2 小时前
(九)Spring Cloud Alibaba 2023.x:微服务接口文档统一管理与聚合
后端·spring cloud·微服务
Barcke2 小时前
📘 初识 WebFlux
spring boot·后端·spring
JohnYan2 小时前
工作笔记 - 一个浏览器环境适用的类型转换工具
javascript·后端·设计模式