Skip to content

Wails使用Go和Web技术编写桌面应用

  1. 开始 安装GO 官网
支持的平台
Windows 10/11 AMD64/ARM64
MacOS 10.13+ AMD64
MacOS 11.0+ ARM64
Linux AMD64/ARM64
依赖
Wails 有许多安装前需要的常见依赖项:
Go 1.18+ //安装完成后可通过 go version 查看版本号
NPM (Node 15+)
  1. 安装Wails
运行 go install github.com/wailsapp/wails/v2/cmd/wails@latest 安装 Wails CLI。
注意1:如果您遇到了类似于以下内容的错误:
....\Go\pkg\mod\github.com\wailsapp\wails\[email protected]\pkg\templates\templates.go:28:12: pattern all:ides/*: no matching files found
请检查您是否已安装 Go 1.18+ ︰
go version
注意2: 如果出现这种错误
go: github.com/wailsapp/wails/v2/cmd/wails@latest: module github.com/wailsapp/wails/v2/cmd/wails: Get "https://proxy.golang.org/github.com/wailsapp/wails/v2/cmd/wails/@v/list": dial tcp 142.251.42.241:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
原因:国内屏蔽了它的地址 使用国内的代理地址
go env -w GOPROXY=https://goproxy.cn 
然后重新运行

3.使用wails init初始化项目 根据个人喜好选择框架

//项目布局
├── build/ -项目构建目录
│   ├── appicon.png - 应用程序图标
│   ├── darwin/ - Mac 特定的项目文件
│   └── windows/ - Windows 特定的项目文件
├── frontend/ -前端项目文件
├── go.mod - Go module 文件
├── go.sum - Go module 校验文件
├── main.go - 主应用
└── wails.json - 项目配置

Wails文档 4.main.go配置

go
package main
import (
	"embed"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
)
//go:embed all:frontend/dist
var assets embed.FS
func main() {
	// Create an instance of the app structure
	app := NewApp()
	// Create application with options
	err := wails.Run(&options.App{
		Title:  "Wails:测试",//项目标题
		Width:  1024,
		Height: 768,
		MinWidth:1024,
		MinHeight:768,
		// WindowStartState:options.Fullscreen,//全屏
		Frameless:true,//无边框
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},//页面背景色
		OnStartup:        app.startup,
		Bind: []interface{}{
			app,
		},
	})

	if err != nil {
		println("Error:", err.Error())
	}
}

5.wails中js可调用的方法均在frontend文件夹下的wailsjsruntime里面