你好世界
本教程的目的是让你能够使用 Wails 运行最基本应用程序。你将能够
- 创建一个新的 Wails 应用程序
- 构建应用程序
- 运行应用程序
注意
本教程使用 Windows 作为目标平台。输出可能会因你的操作系统而略有不同。
创建一个新的 Wails 应用程序
要使用默认的普通 JS 模板创建一个新的 Wails 应用程序,你需要运行以下命令
wails init -n helloworld
你应该看到类似以下内容
Wails CLI v2.0.0
Initialising Project 'helloworld'
---------------------------------
Project Name: helloworld
Project Directory: C:\Users\leaan\tutorial\helloworld
Project Template: vanilla
Template Support: https://wails.golang.ac.cn
Initialised project 'helloworld' in 232ms.
这将在当前目录中创建一个名为 helloworld
的新目录。在这个目录中,你会找到许多文件
build/ - Contains the build files + compiled application
frontend/ - Contains the frontend files
app.go - Contains the application code
main.go - The main program with the application configuration
wails.json - The project configuration file
go.mod - The go module file
go.sum - The go module checksum file
构建应用程序
要构建应用程序,请切换到新的 helloworld
项目目录并运行以下命令
wails build
你应该看到类似以下内容
Wails CLI v2.0.0
App Type: desktop
Platforms: windows/amd64
Compiler: C:\Users\leaan\go\go1.18.3\bin\go.exe
Build Mode: Production
Devtools: false
Skip Frontend: false
Compress: false
Package: true
Clean Build Dir: false
LDFlags: ""
Tags: []
Race Detector: false
Building target: windows/amd64
------------------------------
- Installing frontend dependencies: Done.
- Compiling frontend: Done.
- Generating bundle assets: Done.
- Compiling application: Done.
Built 'C:\Users\leaan\tutorial\helloworld\build\bin\helloworld.exe' in 10.616s.
这已编译了应用程序并将其保存到 build/bin
目录中。
运行应用程序
如果我们在 Windows 资源管理器中查看 build/bin
目录,我们应该看到我们的项目二进制文件
我们可以通过简单地双击 helloworld.exe
文件来运行它。
在 Mac 上,Wails 生成一个 helloworld.app
文件,可以通过双击运行它。
在 Linux 上,你可以使用 build/bin
目录中的 ./helloworld
运行应用程序。
你应该看到应用程序按预期工作
