Golang wails2 轻量级跨端桌面解决方案

wails2

github: github.com/wailsapp/wails

文档: wails.io/zh-Hans/docs/reference/op...

环境要求

  • Go 1.16
  • npm
  • 需是 WindowsMacOS, Linux 操作系统之一
  • gcc

安装

# Go 1.17 下 安装
go install github.com/wailsapp/wails/cmd/wails@latest

# Go 1.16 
$ go get -u github.com/wailsapp/wails/cmd/wails

输入命令,有如下结果就是安装成功了

$ wails update
Wails v1.16.9 - Checking for updates...

> Checking for updates...

    Current Version : v1.16.9
     Latest Release : v1.16.9
Looks like you're up to date!

初始化 个人信息

$ wails setup
 _       __      _ __
| |     / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__  )  v1.16.9
|__/|__/\__,_/_/_/____/   https://wails.app
The lightweight framework for web-like apps

Welcome to Wails!

## 输入开发者姓名 以及 邮箱
What is your name (xiaobaiyaoshengfa):
What is your email address (xiaobaiyaoshengfa@sifou): xiaobaiyaoshengfa@sifou

Wails config saved to: C:\Users\xxx\.wails\wails.json
Feel free to customise these settings.

## 检查当前运行的系统环境,以及依赖的命令行工具是否可用。
Detected Platform: Windows
Checking for prerequisites...
Program 'gcc' found: C:\Program Files\mingw-w64\x86_64-7.3.0-release-posix-seh-rt_v5-rev0\mingw64\bin\gcc.exe
Program 'npm' found: C:\nodejs\npm.cmd

Ready for take off!
Create your first project by running 'wails init'.

Hello World

初始化项目

$ wails init
Wails v1.16.9 - Initialising project

The name of the project (My Project): Hello
Project Name: Hello
The output binary name (hello): hello
Output binary Name: hello
Project directory name (hello):
Project Directory: hello
Please select a template (* means unsupported on current platform):
  1: Angular - Angular 8 template (Requires node 10.8+)
  2: React JS - Create React App v4 template
  3: Svelte - A basic Svelte template
  4: Vanilla - A Vanilla HTML/JS template
  5: * Vue3 Full - Vue 3, Vuex, Vue-router, and Webpack4
  6: * Vue3 JS - A template based on Vue 3, Vue-router, Vuex, and Webpack5
  7: Vue2/Webpack Basic - A basic Vue2/WebPack4 template
  8: Vuetify1.5/Webpack Basic - A basic Vuetify1.5/Webpack4 template
  9: Vuetify2/Webpack Basic - A basic Vuetify2/Webpack4 template

## 这里提供了9个前端模板,比如常见的有 Angular, React,Vue 等,这里我选择了熟悉的 React
Please choose an option [1]: 2
Template: React JS
> Generating project...

## 当前目录下生成一个 hello 文件夹,实际的wails 项目将就在里面。 
Project 'Hello' initialised. Run `wails build` to build it.

项目 目录结构

$ tree -L 2 ./hello/
./hello/
├── appicon.png
├── build
│   └── hello.exe
├── frontend
│   ├── build
│   ├── node_modules
│   ├── package.json
│   ├── package.json.md5
│   ├── package-lock.json
│   ├── public
│   ├── README.md
│   └── src
├── go.mod
├── go.sum
├── hello.exe.manifest
├── hello.ico
├── hello.rc
├── hello-res.syso
├── main.go
└── project.json

6 directories, 14 files

这里 frontend 就是前端项目的目录, ./frontend/build 文件夹保存了 npm 编译后的成果。而 wails 是怎样将其引入到代码中的呢?

打开文件 ./main.go

package main

import (
  _ "embed"
  "github.com/wailsapp/wails"
)

func basic() string {
  return "World!"
}

//go:embed frontend/build/static/js/main.js
var js string

//go:embed frontend/build/static/css/main.css
var css string

func main() {

  app := wails.CreateApp(&wails.AppConfig{
    Width:  1024,
    Height: 768,
    Title:  "Hello",
    JS:     js,
    CSS:    css,
    Colour: "#131313",
  })
  app.Bind(basic)
  app.Run()
}

main.go 文件中可以看到 //go:embed 这两句代码,这用到了 go 1.16 开始官方提供的 embed 新特性,通过这两个指令将 js,css 文件里的代码赋值给 jscss 两个字符串变量。

所以, wails 对go最低版本要求是 1.16

//go:embed 指令,可以在编译阶段将静态资源文件打包进编译好的程序中,并提供访问这些文件的能力。

构建项目

## 这里需要进入 wails 项目
$ cd ./hello

## 项目内进行构建
$ wails build
Wails v1.16.9 - Building Application

> Skipped frontend dependencies (-f to force rebuild)
> Building frontend...
> Ensuring Dependencies are up to date...
> Packing + Compiling project...
*** Please note: Windows builds use mshtml which is only compatible with IE11. For more information, please read https://wails.app/guides/windows/ ***
Awesome! Project 'Hello' built!

运行项目

$ ./build/hello.exe

windows 下会看到 如下结果:

wails 技术分析

IE11

wails build 时,我们就能看见,提示语: IE11,也就是 web-viewerwindows 平台是基于IE11,而在 2021年8月17日,微软就宣布 Office 365 不再考虑在 IE11 上的兼容性问题,并且过滤用户过度到 Edge,但 Edge 不是默认安装。所以 wails 调用 IE11 渲染了web。

至于 ReactVueAngularIE11 的兼容性策略,咋们避开不谈。

Internet Explorer 11 的支持

可执行文件

好,我们再来看看编译文件,才 8MB , 这相比动辄 70MB, 上百兆的 Electron 确实是好太多。我想大家用这个方案就是冲这个来的吧。

Golang wails2 轻量级跨端桌面解决方案

总结

总的来说,wails2 完善了 go 基于 web-view 跨平台桌面程序解决方案。其中,能够使用web前端带来的便利的同时,还能让编译成果尽可能小,这是它最大的亮点。如果你做的东西比较简单,不接依页面的兼容性问题,那么它不失为一个不错的跨端桌面程序解决方案。

常见问题

  • windows 平台下 setupgcc 找不到
Detected Platform: Windows
Checking for prerequisites...
Error: Program 'gcc' not found. Please install gcc from here and try again: http://tdm-gcc.tdragon.net/download. You will need to add the bin directory to your path, EG: C:\TDM-GCC-64\bin\
Program 'npm' found: C:\nodejs\npm.cmd

这里wails检查到了操作系统是 Windows,没有检查到 gcc,这里需要通过 mingw 安装下 gcc,并配置到 windowspath 环境变量。

  • package embed is not in GOROOT
C:\Users\xxx\go\pkg\mod\github.com\wailsapp\wails@v1.16.9\runtime\assets.go:3:8: package embed is not in GOROOT (C:\Users\xxx\.g\go\src\embed)

这是因为你使用的Golang 版本过低导致的,升级到 Go 1.16 就能解决。

  • missing go.sum entry for module providing package golang.org/x/text/transform
C:\Users\xxx\go\pkg\mod\github.com\wailsapp\wails@v1.16.9\runtime\window.go:13:2: missing go.sum entry for module providing package golang.org/x/text/transform (imported by github.com/wailsapp/wails/runtime); to add:
    go get github.com/wailsapp/wails/runtime@v1.16.9

升级到 Go 1.17 可解决,具体原因我也不清楚,有会的大佬告诉我一下。

参考

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!