wails2

环境要求

WindowsMacOSLinux

安装

# 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/buildnpmwails
./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:embedgo 1.16embedjscss
wails1.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 buildweb-viewerwindowsIE11Office 365IE11EdgeEdgewailsIE11
ReactVueAngularIE11

可执行文件

8MB70MBElectron

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

总结

wails2goweb-view

常见问题

windowssetupgcc
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
Windowsgccmingwgccwindowspath
  • 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)
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 协议》,转载必须注明作者和本文链接