GO语言学习笔记(二) - GUI For Go(基于Electron)

GO语言学习笔记(二) - GUI For Go(基于Electron)

前言

网上找了一下Golang的GUI,然后找到了一个基于Electron的介绍。
一开始觉得应该比较简单,但是还是碰到各种坑。
所以将碰到的一些坑和解决方法在这里记录一下。

使用Eletron作为Golang GUI的文章链接:
https://studygolang.com/articles/12065?fr=sidebar

简介

基于Electron,使用go-astilectron作为Golang和Electron的连接媒介。

go-astilectron Github地址:
https://github.com/asticode/go-astilectron

go-astilectron示例 Github地址:
https://github.com/asticode/go-astilectron-demo

配置

在GOPATH下新建 src, pkg, bin三个文件夹

执行Demo提供的命令

Github上Demo的readme中的命令

安装示例

下面的命令都是在PowerShell6.1.0-preview.1中执行的
操作过程中忘了关掉VPN了,在不的情况下不能保证一定能成功。

  • 下载代码
go get -u github.com/asticode/go-astilectron-demo/...

然后就出错误了。
错误信息如下:

PS C:\Program Files\PowerShell\6.1.0-preview.1> go get -u github.com/asticode/go
-astilectron-demo/...
package github.com/asticode/go-astilectron-demo
        imports golang.org/x/crypto/ssh/terminal: unrecognized import path "gola
ng.org/x/crypto/ssh/terminal" (https fetch: Get https://golang.org/x/crypto/ssh/
terminal?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt fa
iled because the connected party did not properly respond after a period of time
, or established connection failed because connected host has failed to respond.
)
PS C:\Program Files\PowerShell\6.1.0-preview.1>

解决方法:

1 在GOPATH目录下的src目录新建目录golang.org,在golang.org目录下新建目录x,并进入到新建的目录x中。

PS C:\Program Files\PowerShell\6.1.0-preview.1> cd $GOPATH
PS C:\Users\Administrator> cd go
PS C:\Users\Administrator\go> cd src
PS C:\Users\Administrator\go\src> mkdir golang.org
略(此行非命令)
PS C:\Users\Administrator\go\src> cd golang.org
PS C:\Users\Administrator\go\src\golang.org> mkdir x
略(此行非命令)
PS C:\Users\Administrator\go\src\golang.org> cd x
PS C:\Users\Administrator\go\src\golang.org\x>

2.执行命令下载crypto。
注:前提是已安装Git。

git clone https://github.com/golang/crypto.git

执行过程

PS C:\Users\Administrator\go\src\golang.org\x> git clone https://github.com/gola
ng/crypto.git
Cloning into 'crypto'...
remote: Counting objects: 4615, done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 4615 (delta 8), reused 11 (delta 3), pack-reused 4580
Receiving objects: 100% (4615/4615), 3.64 MiB | 43.00 KiB/s, done.
Resolving deltas: 100% (2933/2933), done.
PS C:\Users\Administrator\go\src\golang.org\x>
  • 继续执行下载代码的命令
go get -u github.com/asticode/go-astilectron-demo/...

然后继续出错。

PS C:\Program Files\PowerShell\6.1.0-preview.1> go get -u github.com/asticode/go
-astilectron-demo/...
package golang.org/x/sys/windows: unrecognized import path "golang.org/x/sys/win
dows" (https fetch: Get https://golang.org/x/sys/windows?go-get=1: dial tcp 216.
239.37.1:443: connectex: A connection attempt failed because the connected party
 did not properly respond after a period of time, or established connection fail
ed because connected host has failed to respond.)
PS C:\Program Files\PowerShell\6.1.0-preview.1>

解决方法:

进入到上面说的golang.org/x目录下,执行命令下载sys。
注:前提是已安装Git。

git clone https://github.com/golang/sys.git

执行过程

PS C:\Users\Administrator\go\src\golang.org\x> git clone https://github.com/gola
ng/sys.git
Cloning into 'sys'...
remote: Counting objects: 5475, done.
remote: Compressing objects: 100% (167/167), done.
remote: Total 5475 (delta 225), reused 231 (delta 167), pack-reused 5141
Receiving objects: 100% (5475/5475), 4.38 MiB | 35.00 KiB/s, done.
Resolving deltas: 100% (4609/4609), done.
PS C:\Users\Administrator\go\src\golang.org\x>
  • 继续执行下载命令
    出现下面的信息即为下载成功,其中的undefined: xxx 可忽略。
PS C:\Program Files\PowerShell\6.1.0-preview.1> go get -u github.com/asticode/go
-astilectron-demo/...
# github.com/asticode/go-astilectron-demo
C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo\main.go:35
:13: undefined: Asset
C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo\main.go:36
:13: undefined: AssetDir
C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo\main.go:76
:18: undefined: RestoreAssets
PS C:\Program Files\PowerShell\6.1.0-preview.1>
  • 删除bind.go
    进入到go-astilectron-demo所在目录,删除bind.go文件。

好像不删除会出问题。
不一定非得使用命令删除。

PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo> rm bin
d.go
PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo>

安装bundler

!需要把$GOPATH/bin添加到系统环境变量$PATH中,不然会提示找不到命令!

Windows的系统环境变量PATH

%GOPATH%\bin;

执行命令安装bundler

go get -u github.com/asticode/go-astilectron-bundler/...

执行过程

PS C:\Program Files\PowerShell\6.1.0-preview.1> go get -u github.com/asticode/go
-astilectron-bundler/...
PS C:\Program Files\PowerShell\6.1.0-preview.1>

在当前环境中bundle应用程序

  • bundle应用程序
    进入到go-astilectron-demo目录,执行bundle命令。
astilectron-bundler -v

执行过程略漫长,然后在下载electron的时候会出错误。

PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo> astile
ctron-bundler -v
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\Administrator\\go\\src
\\github.com\\asticode\\go-astilectron-demo\\output","time":"2018-08-30T13:32:11
+08:00"}
{"app_name":"","level":"debug","msg":"Bundling for environment windows/amd64","t
ime":"2018-08-30T13:32:12+08:00"}
{"app_name":"","level":"debug","msg":"Binding data","time":"2018-08-30T13:32:12+
08:00"}
{"app_name":"","level":"debug","msg":"Removing C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind","time":"2018-08-30T13:32:12+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind","time":"2018-08-30T13:32:12+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind\\vendor_astilectron_bundler","time":"2018-08-30T13:3
2:12+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\cache","time":"2018-08-30T13:32:12+08:00"}
{"app_name":"","level":"debug","msg":"Downloading https://github.com/asticode/as
tilectron/archive/v0.27.0.zip into C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\as
tibundler\\cache\\astilectron-0.27.0.zip","time":"2018-08-30T13:32:12+08:00"}
{"app_name":"","level":"debug","msg":"Copying C:\\Users\\ADMINI~1\\AppData\\Loca
l\\Temp\\astibundler\\cache\\astilectron-0.27.0.zip to C:\\Users\\ADMINI~1\\AppD
ata\\Local\\Temp\\astibundler\\bind\\vendor_astilectron_bundler\\astilectron.zip
","time":"2018-08-30T13:32:17+08:00"}
{"app_name":"","level":"debug","msg":"Downloading https://github.com/electron/el
ectron/releases/download/v1.8.1/electron-v1.8.1-win32-x64.zip into C:\\Users\\AD
MINI~1\\AppData\\Local\\Temp\\astibundler\\cache\\electron-windows-amd64-1.8.1.z
ip","time":"2018-08-30T13:32:18+08:00"}
{"app_name":"","level":"debug","msg":"Removing C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\cache\\electron-windows-amd64-1.8.1.zip...","time":"2018-
08-30T13:39:17+08:00"}
{"app_name":"","level":"fatal","msg":"bundling failed: bundling for environment
windows/amd64 failed: binding data failed: provisioning the vendor failed: provi
sioning electron vendor for OS windows and arch amd64 failed: downloading https:
//github.com/electron/electron/releases/download/v1.8.1/electron-v1.8.1-win32-x6
4.zip into C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\astibundler\\cache\\electr
on-windows-amd64-1.8.1.zip failed: astihttp.Download failed: astihttp: downloadi
ng in writer failed: astihttp: copying content from https://github.com/electron/
electron/releases/download/v1.8.1/electron-v1.8.1-win32-x64.zip to writer failed
: read tcp 192.168.30.12:6410-\u003e52.216.86.27:443: wsarecv: An existing conne
ction was forcibly closed by the remote host.","time":"2018-08-30T13:39:17+08:00
"}
PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo>

解决方法:
分析命令行bundler的日志,可以发现,需要下载electron-v1.8.1-win32-x64.zip。
从日志中复制下载链接:
https://github.com/electron/electron/releases/download/v1.8.1/electron-v1.8.1-win32-x64.zip
把下载的文件重命名为日志中指定的文件名electron-windows-amd64-1.8.1.zip。
然后把文件放到下面目录中

C:\Users\Administrator\AppData\Local\Temp\astibundler\cache
  • 再次执行bundler命令
astilectron-bundler -v

执行过程

PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo> astile
ctron-bundler -v
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\Administrator\\go\\src
\\github.com\\asticode\\go-astilectron-demo\\output","time":"2018-08-30T14:11:59
+08:00"}
{"app_name":"","level":"debug","msg":"Bundling for environment windows/amd64","t
ime":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Binding data","time":"2018-08-30T14:11:59+
08:00"}
{"app_name":"","level":"debug","msg":"Removing C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind\\vendor_astilectron_bundler","time":"2018-08-30T14:1
1:59+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\cache","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\
\astibundler\\cache\\astilectron-0.27.0.zip already exists, skipping download of
 https://github.com/asticode/astilectron/archive/v0.27.0.zip","time":"2018-08-30
T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Copying C:\\Users\\ADMINI~1\\AppData\\Loca
l\\Temp\\astibundler\\cache\\astilectron-0.27.0.zip to C:\\Users\\ADMINI~1\\AppD
ata\\Local\\Temp\\astibundler\\bind\\vendor_astilectron_bundler\\astilectron.zip
","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\
\astibundler\\cache\\electron-windows-amd64-1.8.1.zip already exists, skipping d
ownload of https://github.com/electron/electron/releases/download/v1.8.1/electro
n-v1.8.1-win32-x64.zip","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Copying C:\\Users\\ADMINI~1\\AppData\\Loca
l\\Temp\\astibundler\\cache\\electron-windows-amd64-1.8.1.zip to C:\\Users\\ADMI
NI~1\\AppData\\Local\\Temp\\astibundler\\bind\\vendor_astilectron_bundler\\elect
ron.zip","time":"2018-08-30T14:11:59+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\ADMINI~1\\AppData\\Loc
al\\Temp\\astibundler\\bind\\resources","time":"2018-08-30T14:12:03+08:00"}
{"app_name":"","level":"debug","msg":"Copying C:\\Users\\Administrator\\go\\src\
\github.com\\asticode\\go-astilectron-demo\\resources to C:\\Users\\ADMINI~1\\Ap
pData\\Local\\Temp\\astibundler\\bind\\resources","time":"2018-08-30T14:12:03+08
:00"}
{"app_name":"","level":"debug","msg":"Generating C:\\Users\\Administrator\\go\\s
rc\\github.com\\asticode\\go-astilectron-demo\\bind_windows_amd64.go","time":"20
18-08-30T14:12:04+08:00"}
{"app_name":"","level":"debug","msg":"Running rsrc for icon C:\\Users\\Administr
ator\\go\\src\\github.com\\asticode\\go-astilectron-demo\\resources\\icon.ico in
to C:\\Users\\Administrator\\go\\src\\github.com\\asticode\\go-astilectron-demo\
\windows.syso","time":"2018-08-30T14:12:07+08:00"}
{"app_name":"","level":"debug","msg":"Removing C:\\Users\\Administrator\\go\\src
\\github.com\\asticode\\go-astilectron-demo\\output\\windows-amd64","time":"2018
-08-30T14:12:07+08:00"}
{"app_name":"","level":"debug","msg":"Creating C:\\Users\\Administrator\\go\\src
\\github.com\\asticode\\go-astilectron-demo\\output\\windows-amd64","time":"2018
-08-30T14:12:07+08:00"}
{"app_name":"","level":"debug","msg":"Building for os windows and arch amd64","t
ime":"2018-08-30T14:12:07+08:00"}
{"app_name":"","level":"debug","msg":"Executing go build -ldflags -s -X \"main.A
ppName=Astilectron demo\" -X \"main.BuiltAt=2018-08-30 14:12:07.1812578 +0800 CS
T m=+7.821360901\" -H windowsgui -o C:\\Users\\Administrator\\go\\src\\github.co
m\\asticode\\go-astilectron-demo\\output\\windows-amd64\\binary github.com\\asti
code\\go-astilectron-demo","time":"2018-08-30T14:12:07+08:00"}
{"app_name":"","level":"debug","msg":"Moving C:\\Users\\Administrator\\go\\src\\
github.com\\asticode\\go-astilectron-demo\\output\\windows-amd64\\binary to C:\\
Users\\Administrator\\go\\src\\github.com\\asticode\\go-astilectron-demo\\output
\\windows-amd64\\Astilectron demo.exe","time":"2018-08-30T14:12:27+08:00"}
PS C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo>

执行编译后的程序

bundler命令执行成功后,会在程序所在目录下生成下面的目录。
output/-
我的目录是

C:\Users\Administrator\go\src\github.com\asticode\go-astilectron-demo\output\windows-amd64

目录中的文件是编译好的文件,文件名是Astilectron demo.exe。
双击执行,大功告成!!
GO语言学习笔记(二) - GUI For Go(基于Electron)_第1张图片

你可能感兴趣的:(GO语言学习笔记(二) - GUI For Go(基于Electron))