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))
}
相关推荐
Warren981 小时前
Java面试八股Spring篇(4500字)
java·开发语言·spring boot·后端·spring·面试
背帆1 小时前
go的interface接口底层实现
开发语言·后端·golang
IT成长史2 小时前
deepseek梳理java高级开发工程师springboot面试题2
java·spring boot·后端
qq_266348733 小时前
springboot AOP中,通过解析SpEL 表达式动态获取参数值
java·spring boot·后端
bing_1583 小时前
MQTT 在Spring Boot 中的使用
java·spring boot·后端·mqtt
阑梦清川6 小时前
关于Go语言的开发环境的搭建
开发语言·后端·golang
lyrhhhhhhhh6 小时前
Spring 模拟转账开发实战
java·后端·spring
言之。6 小时前
Makefile 在 Go 项目中的实践
开发语言·elasticsearch·golang
tonngw7 小时前
【Mac 从 0 到 1 保姆级配置教程 12】- 安装配置万能的编辑器 VSCode 以及常用插件
git·vscode·后端·macos·开源·编辑器·github
noravinsc8 小时前
InforSuite RDS 与django结合
后端·python·django