Lorca

用Go+HTML5构建跨平台的现代桌面应用。「Build cross-platform modern desktop apps in Go + HTML5」

  • 所有者: zserge/lorca
  • 平台: Linux, Mac, Windows
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Lorca

一个非常小的库,用于在 Go 中构建现代 HTML5 桌面应用。它使用 Chrome 浏览器作为 UI 层。与 Electron 不同的是,它并没有将 Chrome 浏览器捆绑到应用包中,而是重复使用已经安装的 Chrome 浏览器。Lorca 建立了与浏览器窗口的连接,允许从用户界面调用 Go 代码,并以无缝方式从 Go 中操作用户界面。

功能介绍

  • 纯粹的 Go 库(没有 cgo),具有非常简单的 API。
  • 应用体积小(通常为 5-10MB)
  • 两全其美 -- HTML/CSS 的全部功能使您的UI看起来很好,同时结合了 Go 的性能和易于开发的特点。
  • 公开 Go 函数/方法,并从 JavaScript 中调用它们。
  • 从 Go 中调用任意的 JavaScript 代码
  • 两种语言的 UI 和主应用程序之间的异步流(async/await 和 Goroutines)。
  • 支持从本地 web 服务器或通过数据 URL 加载 web UI。
  • 支持在无头模式下用用户界面测试您的应用程序。
  • 支持多个应用程序窗口
  • 支持包装和品牌(如自定义应用图标)。使用 GOOS 和 GOARCH 变量可以在一台机器上完成三个 OS 的包装。

另外,受设计限制:

  • 需要安装 Chrome/Chromium>=70 才能使用。
  • 还不能控制 Chrome 窗口(如不能去除边框,使其透明,控制位置和大小)。
  • 没有窗口菜单(托盘菜单和原生操作系统对话框仍然可以通过第三方库来实现)。

如果你想对浏览器窗口有更多的控制 -- 可以考虑使用类似 API 的 webview 库,这样迁移就会很顺利。

例子

ui, _ := lorca.New("", "", 480, 320)
defer ui.Close()
// Bind Go function to be available in JS. Go function may be long-running and
// blocking - in JS it's represented with a Promise.
ui.Bind("add", func(a, b int) int { return a + b })
// Call JS function from Go. Functions may be asynchronous, i.e. return promises
n := ui.Eval(`Math.random()`).Float()
fmt.Println(n)
// Call JS that calls Go and so on and so on...
m := ui.Eval(`add(2, 3)`).Int()
fmt.Println(m)
// Wait for the browser window to be closed
<-ui.Done()

另外,关于绑定函数和打包二进制文件的更多细节,请参见 示例

Hello World

下面是运行 hello world 示例的步骤:

cd examples/counter
go get
go run ./

它是如何工作的

在后台,Lorca 使用 Chrome DevTools 协议 对 Chrome 实例进行检测。首先,Lorca 尝试定位您安装的 Chrome 浏览器,启动一个绑定到短暂端口的远程调试实例,并从 stderr 中读取实际 WebSocket 端点。然后 Lorca 打开一个新的客户端连接到 WebSocket 服务器,并通过 WebSocket 发送 Chrome DevTools 协议方法的 JSON 消息来检测 Chrome。JavaScript 函数在 Chrome 浏览器中评估,而 Go 函数实际上在 Go 运行时运行,并将返回值发送到 Chrome 浏览器。

名字里有什么?

有一种传说,在他被处死之前,加西亚-洛尔卡看到了士兵头上的日出,他说:"然而,太阳升起......"。可能是一首诗的开头。(J. Brodsky)

Lorca 是 Carlo 的异形词,是 Node.js 的一个类似目标的项目。

许可证

代码是在 MIT 授权下发布的,您也可以在您的专有项目中自由使用它。


主要指标

概览
名称与所有者zserge/lorca
主编程语言Go
编程语言Go (语言数: 1)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2018-11-04 21:40:19
推送于2024-08-19 06:48:40
最后一次提交2024-01-07 12:48:49
发布数11
最新版本名称v0.1.10 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数8.1k
关注者数150
派生数547
提交数104
已启用问题?
问题数139
打开的问题数68
拉请求数32
打开的拉请求数8
关闭的拉请求数9
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

Lorca

Build Status
GoDoc
Go Report Card

Features

  • Pure Go library (no cgo) with a very simple API
  • Small application size (normally 5-10MB)
  • Best of both worlds - the whole power of HTML/CSS to make your UI look
    good, combined with Go performance and ease of development
  • Expose Go functions/methods and call them from JavaScript
  • Call arbitrary JavaScript code from Go
  • Asynchronous flow between UI and main app in both languages (async/await and Goroutines)
  • Supports loading web UI from the local web server or via data URL
  • Supports embedding all assets into a single binary
  • Supports testing your app with the UI in the headless mode
  • Supports multiple app windows
  • Supports packaging and branding (e.g. custom app icons). Packaging for all
    three OS can be done on a single machine using GOOS and GOARCH variables.

Also, limitations by design:

  • Requires Chrome/Chromium >= 70 to be installed.
  • No control over the Chrome window yet (e.g. you can't remove border, make it
    transparent, control position or size).
  • No window menu (tray menus and native OS dialogs are still possible via
    3rd-party libraries)

If you want to have more control of the browser window - consider using
webview library with a similar API, so
migration would be smooth.

Example

ui, _ := lorca.New("", "", 480, 320)
defer ui.Close()

// Bind Go function to be available in JS. Go function may be long-running and
// blocking - in JS it's represented with a Promise.
ui.Bind("add", func(a, b int) int { return a + b })

// Call JS function from Go. Functions may be asynchronous, i.e. return promises
n := ui.Eval(`Math.random()`).Float()
fmt.Println(n)

// Call JS that calls Go and so on and so on...
m := ui.Eval(`add(2, 3)`).Int()
fmt.Println(m)

// Wait for the browser window to be closed
<-ui.Done()

Also, see examples for more details about binding functions, embedding
assets and packaging binaries.

Hello World

Here are the steps to run the hello world example.

cd examples/counter
go get
go run main.go

How it works

Under the hood Lorca uses Chrome DevTools Protocol to instrument on a Chrome instance. First Lorca tries to locate your installed Chrome, starts a remote debugging instance binding to an ephemeral port and reads from stderr for the actual WebSocket endpoint. Then Lorca opens a new client connection to the WebSocket server, and instruments Chrome by sending JSON messages of Chrome DevTools Protocol methods via WebSocket. JavaScript functions are evaluated in Chrome, while Go functions actually run in Go runtime and returned values are sent to Chrome.

What's in a name?

There is kind of a legend, that before his execution Garcia Lorca have seen a
sunrise over the heads of the soldiers and he said "And yet, the sun rises...".
Probably it was the beginning of a poem. (J. Brodsky)

Lorca is an anagram of Carlo, a
project with a similar goal for Node.js.

License

Code is distributed under MIT license, feel free to use it in your proprietary
projects as well.