使用Golang和wails开发桌面程序

参考文档

基础必备条件

  • Go 1.21+ (macOS 15+ requires Go 1.23.3+)
  • NPM (Node 15+)

go 和 nodejs 的安装和环境变量配置请查看本网站的其他文档。

不同环境的安装要求请参考官网文档: https://wails.io/docs/gettingstarted/installation

Wails 的安装

直接通过 go 命令安装 Wails 可执行程序

1$ go install github.com/wailsapp/wails/v2/cmd/wails@latest

这样在 gopath 的 bin 目录下生成了 wails.exe

检查安装环境是否缺乏相应的依赖

重点关注自己golang版本、node版本等关键信息。

 1$ wails doctor
 2
 3
 4          Wails Doctor
 5
 6
 7
 8# Wails
 9Version | v2.11.0
10
11
12# System
13┌──────────────────────────────────────────────────────────────────────────────────────┐
14| OS           | Windows 10 Pro                                                        |
15| Version      | 2009 (Build: 19045)                                                   |
16| ID           | 22H2                                                                  |
17| Branding     | Windows 10 专业版                                                     |
18| Go Version   | go1.24.0                                                              |
19| Platform     | windows                                                               |
20| Architecture | amd64                                                                 |
21| CPU          | Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz                               |
22| GPU 1        | NVIDIA GeForce GTX 1660 Ti (NVIDIA) - Driver: 27.21.14.5720           |
23| GPU 2        | Intel(R) UHD Graphics 630 (Intel Corporation) - Driver: 31.0.101.2111 |
24| Memory       | 24GB                                                                  |
25└──────────────────────────────────────────────────────────────────────────────────────┘
26
27# Dependencies
28┌───────────────────────────────────────────────────────┐
29| Dependency | Package Name | Status    | Version       |
30| WebView2   | N/A          | Installed | 142.0.3595.53 |
31| Nodejs     | N/A          | Installed | 20.15.0       |
32| npm        | N/A          | Installed | 10.7.0        |
33| *upx       | N/A          | Available |               |
34| *nsis      | N/A          | Available |               |
35|                                                       |
36└─────────────── * - Optional Dependency ───────────────┘
37
38# Diagnosis
39Optional package(s) installation details:
40  - upx : Available at https://upx.github.io/
41  - nsis : More info at https://wails.io/docs/guides/windows-installer/
42
43 SUCCESS  Your system is ready for Wails development!
44
45 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
46https://github.com/sponsors/leaanthony

如果显示 SUCCESS Your system is ready for Wails development! 表示环境没有问题了。

桌面开发示例

初始化 wails 项目

首先需要初始化 wails 项目,帮助生成基础目录和框架:

1$ wails init -n "My-Wails-Tools" -t vue -ide goland -d D:/dev/workspaces/my-wails-tools

其中:

  • -t 表示开发模板,就是使用什么框架进行界面开发,常用前端模板选项:
    • vanilla - 纯JavaScript
    • vue - Vue框架
    • vue-ts - Vue + TypeScript
    • react - React框架
    • react-ts - React + TypeScript
    • preact - Preact框架
    • preact-ts - Preact + TypeScript
    • svelte - Svelte框架
    • svelte-ts - Svelte + TypeScript
    • lit - Lit框架
    • solid - SolidJS框架
  • -ide 指定开发的IDE;常用IDE选项:
    • vscode - Visual Studio Code
    • goland - GoLand
  • -d 表示生成项目的目录(相对目录和绝对目录都行)

生成的目录结构如下:

 1$ tree
 2.
 3├── README.md
 4├── app.go
 5├── build
 6│   ├── README.md
 7│   ├── appicon.png
 8│   ├── darwin
 9│   │   ├── Info.dev.plist
10│   │   └── Info.plist
11│   └── windows
12│       ├── icon.ico
13│       ├── info.json
14│       ├── installer
15│       │   ├── project.nsi
16│       │   └── wails_tools.nsh
17│       └── wails.exe.manifest
18├── frontend
19│   ├── README.md
20│   ├── dist
21│   │   └── gitkeep
22│   ├── index.html
23│   ├── package.json
24│   ├── src
25│   │   ├── App.vue
26│   │   ├── assets
27│   │   │   ├── fonts
28│   │   │   │   ├── OFL.txt
29│   │   │   │   └── nunito-v16-latin-regular.woff2
30│   │   │   └── images
31│   │   │       └── logo-universal.png
32│   │   ├── components
33│   │   │   └── HelloWorld.vue
34│   │   ├── main.js
35│   │   └── style.css
36│   ├── vite.config.js
37│   └── wailsjs
38│       ├── go
39│       │   └── main
40│       │       ├── App.d.ts
41│       │       └── App.js
42│       └── runtime
43│           ├── package.json
44│           ├── runtime.d.ts
45│           └── runtime.js
46├── go.mod
47├── go.sum
48├── main.go
49└── wails.json
50
5116 directories, 32 files

其中:

  • /main.go - 主应用程序
  • /frontend/ - 前端项目文件
  • /build/ - 项目构建目录
  • /build/appicon.png - 应用程序图标
  • /build/darwin/ - Mac 特定的项目文件
  • /build/windows/ - Windows 特定的项目文件
  • /wails.json - 项目配置
  • /go.mod - Go 模块文件
  • /go.sum - Go 模块校验和文件

调试运行项目

1# 进入项目目录
2$ cd D:/dev/workspaces/my-wails-tools
3
4# 运行
5$ wails dev

构建项目

1$ wails build