我想构建一个本地 Go 桌面应用程序,有几种方法可以做到:Electron[1]:将 Node.js[2] 和 Chromium[3] 浏览器绑定在一起,以创建一个打包的本地 Web 应用程序。可与 Go 框架(例如 go-app[4] 或 go-astilectron[5])一起使用。
Lorca[6]:使用本地安装的 Chrome 浏览器,通过 dev-tools communication protocol[7] 实现。
Webview[8]:使用 webview[9] 创建一个本地窗口,并使用 CGo 绑定在其中渲染应用程序。
我已经写过有关构建一个简单的 electron 应用程序的文章[10],因此本文将探讨如何使用 Lorca 和 Webview 构建应用程序,然后比较这三种的不同。
Lorca
Go 中一个简单的 Lorca[11] 应用:func main() {
// Create UI with data URI
ui, _ := lorca.New("data:text/html,"+url.PathEscape(`
Hello
Hello, world!
`), "", 600, 200)
defer ui.Close()
// Create a GoLang function callable from JS
ui.Bind("hello", func() string { return "World!" })
// Call above `hello` function then log to the JS console
ui.Eval("hello().then( (x) => { console.log(x) })")
// Wait until UI window is closed
}
因为复杂性被隐藏了,所以看起来非常简单!上面的代码打开一个 Chome 窗口,通过 websocket 连接到其 dev-tools[12] 端点,发送要加载的 HTML,并提供 Go 和 JS 之间的通信:
更酷的是,您可以在 Chrome 中调用 JS 函数并在 Go 中获取输