Go,又称Golang,是谷歌在21世纪开发的一种新的编程语言,它静态强类型、从语言层面支持并发(Goroutine)、支持垃圾回收GC。
Go语言有一些笔者很喜欢的特点,譬如跨平台、交叉编译(在某个环境中编译其他平台的程序)、支持并发、语言上做“减法”精简关键字、直接静态编译成单个二进制可执行文件(告别缺少vcruntime.dll的痛苦)。
{}
Wails的思路是使用Web端的技术(html/css/js)作为前端,可以使用js框架如vue和react,后端使用Go语言,前后端交互通过go与js绑定某种方法实现,如此前端便可使用各种成熟的框架,似乎学习web端的技术、Go语言、另一个网页后端的Go语言框架(如Gin)即可打通全栈,想法是好,但是还得一步步学习。
体积较小、功能齐全
hello-world.exe
接下来笔者会把整个学习的过程记录下来,目前只有wails的官网可以提供较好的文档,其中也会涉及纯Golang的知识点,因为笔者也在Go语言学习初期。
环境搭建折腾了很久,最后决定在虚拟机中使用Win10企业版LTS搭建Go+wails的环境,装MacOS出现了兼容问题,linux中也有一些难处、需要大量的时间深入了解linux,故当下选择win10。
有钱了也组台黑苹果吧,windows最大的问题是它不够优雅。
安装Go
下载对应安装包并安装。下载地址
安装Node
下载安装,如此便有了npm。下载地址
再用如下命令安装cnpm,国内使用的速度提升很大。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install -g cnpm --registry=https://registry.npm.taobao.org</pre>
如有其它web端的依赖请自行安装。
安装GCC库
Windows下需要安装GCC库用来编译。下载地址
安装Git Bash
可能会用到Git操作Push/Clone,建议安装。下载地址
安装Docker
官方文档似乎提到Docker在wails交叉编译时会用到。下载地址
安装IDE
这里推荐两个IDE/编辑器:微软的VSCode 和 jetbrains的Goland。
各有优劣,这里笔者用的是Goland,学生使用教育邮箱可白嫖,开源项目也可申请。
IDE的配置略过,注意VSCode可以安装sync插件,把所有配置同步到github gist上,之后到任意机器上安装sync插件粘贴一串代码、登录github即可同步所有插件设置。
设置Goproxy
按照Goproxy官网的说明设置代理:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n68" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct</pre>
其他(非必须)
MactypeNoMeiryoUI
Go语言项目文件结构
GOPATH下共有三个文件夹:bin、pkg、src
bin存放编译好的程序
pkg存放编译后生成的各种文件和各种包(代码中import的外部包)
src存放源代码
一般需要配置GOROOT、GOPATH、GOBIN的环境变量,不过用Goland似乎可以懒一点,因为是虚拟机所以全默认很方便,src文件夹直接发送到桌面快捷方式。
测试纯Go语言
testsrc/test/main.go
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n47" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package main
import(
"fmt"
)
func main() {
fmt.Println("Hello World!")
}</pre>
src/test/go run main.go
go build main.gomain.exe
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n50" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">C:\Users\Purp1e\go\src\test>main.exe
That's Good!</pre>
运行第一个Wails程序
在终端中输入wails,配置一下名字和邮箱。
wails init
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">C:\Users\Purp1e\go\src>wails init
Wails v1.7.1 - Initialising project
The name of the project (My Project): tutorial
Project Name: tutorial
The output binary name (tutorial):
Output binary Name: tutorial
Project directory name (tutorial): tutorial
Project Directory: tutorial
Please select a template:
1: Angular - Angular 8 template (Requires node 10.8+)
2: React JS - Create React App v3 template
3: Vanilla - A Vanilla HTML/JS template
4: Vue2/Webpack Basic - A basic Vue2/WebPack4 template
5: Vuetify1.5/Webpack Basic - A basic Vuetify1.5/Webpack4 template
6: Vuetify2/Webpack Basic - A basic Vuetify2/Webpack4 template
Please choose an option [1]: 4
Template: Vue2/Webpack Basic
Generating project...
Building project (this may take a while)...
Project 'tutorial' built in directory 'tutorial'!</pre>
项目结构如下图:
appicon.png
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n59" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">C:\Users\Purp1e\go\src\tutorial>wails -help
| | / /___ () /____
| | /| / / __ `/ / / /
| |/ |/ / // / / ( ) v1.7.1
|/|/_,///____/ https://wails.app
The lightweight framework for web-like apps
Available commands:
setup Setup the Wails environment [default]
migrate Migrate projects to latest Wails release
init Initialises a new Wails project
build Builds your Wails project
serve Run your Wails project in bridge mode
update Update to newer [pre]releases or specific versions
issue Generates an issue in Github
Flags:
-help
Get help on the 'wails' command.
</pre>
wails buildbuildwails serve
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n61" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">C:\Users\Purp1e\go\src\tutorial>wails build
Wails v1.7.1 - Building Application
Skipped frontend dependencies (-f to force rebuild)
Building frontend...
Ensuring Dependencies are up to date...
Packing + Compiling project...
Awesome! Project 'tutorial' built!
C:\Users\Purp1e\go\src\tutorial>cd build
C:\Users\Purp1e\go\src\tutorial\build>tutorial.exe</pre>
运行结果如图: