GopherJS:从 Go 到 JavaScript 的编译器,用于在浏览器中运行 Go 代码-开源

  • H1_440283
  • 372.8KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-05-27 07:44
GopherJS 将 Go 代码编译为纯 JavaScript 代码。 它的主要目的是让您有机会在 Go 中编写仍可在所有浏览器中运行的前端代码。 几乎所有东西都受支持,包括 Goroutines(兼容性文档)。 大多数情况下性能相当不错,参见 HTML5 游戏引擎基准测试。 不支持 Cgo。 GopherJS 需要 Go 1.16 或更新版本。 如果您需要较旧的 Go 版本,则可以使用较旧的 Gopher 版本。 GopherJS 在生成代码时使用您平台的默认 GOOS 值。 支持的 GOOS 值有:linux、darwin。 如果您在不同的平台上(例如,Windows 或 FreeBSD),您需要将 GOOS 环境变量设置为支持的值。 gopherjs serve 是一个可以在开发过程中使用的有用命令。 默认情况下,它会在“:8080”上启动一个 HTTP 服务器,然后使用 GopherJS 动态编译你的 Go 包并为它们提供服务。
GopherJS 1.16.2 (for Go 1.16.4).zip
GopherJS - A compiler from Go to JavaScript ------------------------------------------- [![GoDoc](]( [![Sourcegraph](]( [![Circle CI](]( GopherJS compiles Go code ([]( to pure JavaScript code. Its main purpose is to give you the opportunity to write front-end code in Go which will still run in all browsers. ### What's new? - 2021-04-04: **Go 1.16 is now officially supported!** 🎉 🎉 🎉 ### Playground Give GopherJS a try on the [GopherJS Playground]( ### What is supported? Nearly everything, including Goroutines ([compatibility documentation]( Performance is quite good in most cases, see [HTML5 game engine benchmark]( Cgo is not supported. ### Installation and Usage GopherJS [requires Go 1.16 or newer]( If you need an older Go version, you can use an [older Gopher release]( Get or update GopherJS and dependencies with: ``` go get -u ``` If your local Go distribution as reported by `go version` is newer than Go 1.16, then you need to set the `GOPHERJS_GOROOT` environment variable to a directory that contains a Go 1.16 distribution. For example: ``` go get go1.16.3 download export GOPHERJS_GOROOT="$(go1.16.3 env GOROOT)" # Also add this line to your .profile or equivalent. ``` Now you can use `gopherjs build [package]`, `gopherjs build [files]` or `gopherjs install [package]` which behave similar to the `go` tool. For `main` packages, these commands create a `.js` file and `` source map in the current directory or in `$GOPATH/bin`. The generated JavaScript file can be used as usual in a website. Use `gopherjs help [command]` to get a list of possible command line flags, e.g. for minification and automatically watching for changes. `gopherjs` uses your platform's default `GOOS` value when generating code. Supported `GOOS` values are: `linux`, `darwin`. If you're on a different platform (e.g., Windows or FreeBSD), you'll need to set the `GOOS` environment variable to a supported value. For example, `GOOS=linux gopherjs build [package]`. *Note: GopherJS will try to write compiled object files of the core packages to your $GOROOT/pkg directory. If that fails, it will fall back to $GOPATH/pkg.* #### gopherjs run, gopherjs test If you want to use `gopherjs run` or `gopherjs test` to run the generated code locally, install Node.js 10.0.0 (or newer), and the `source-map-support` module: ``` npm install --global source-map-support ``` On supported `GOOS` platforms, it's possible to make system calls (file system access, etc.) available. See [doc/]( for instructions on how to do so. #### gopherjs serve `gopherjs serve` is a useful command you can use during development. It will start an HTTP server serving on ":8080" by default, then dynamically compile your Go packages with GopherJS and serve them. For example, navigating to `http://localhost:8080/` should compile and run the Go package ``. The generated JavaScript output will be served at `http://localhost:8080/` (the .js file name will be equal to the base directory name). If the directory contains `index.html` it will be served, otherwise a minimal `index.html` that includes `<script src="project.js"></script>` will be provided, causing the JavaScript to be executed. All other static files will be served too. Refreshing in the browser will rebuild the served files if needed. Compilation errors will be displayed in terminal, and in browser console. Additionally, it will serve $GOROOT and $GOPATH for sourcemaps. If you include an argument, it will be the root from which everything is served. For example, if you run `gopherjs serve` then the generated JavaScript for the package will be served at http://localhost:8080/mypkg/mypkg.js. #### Environment Variables There is one GopherJS-specific environment variable: - `GOPHERJS_GOROOT` - if set, GopherJS uses this value as the default GOROOT value, instead of using the system GOROOT as the default GOROOT value - `GOPHERJS_SKIP_VERSION_CHECK` - if set to true, GopherJS will not check Go version in the GOROOT for compatibility with the GopherJS release. This is primarily useful for testing GopherJS against unreleased versions of Go. ### Performance Tips - Use the `-m` command line flag to generate minified code. - Apply gzip compression ( - Use `int` instead of `(u)int8/16/32/64`. - Use `float64` instead of `float32`. ### Community - [#gopherjs Channel on Gophers Slack]( (invites to Gophers Slack are available [here]( - [Bindings to JavaScript APIs and libraries]( - [GopherJS Blog]( - [GopherJS on Twitter]( ### Getting started #### Interacting with the DOM The package `` (see [documentation]( provides functions for interacting with native JavaScript APIs. For example the line ```js document.write("Hello world!"); ``` would look like this in Go: ```go js.Global.Get("document").Call("write", "Hello world!") ``` You may also want use the [DOM bindings](, the [jQuery bindings]( (see [TodoMVC Example]( or the [AngularJS bindings]( Those are some of the [bindings to JavaScript APIs and libraries]( by community members. #### Providing library functions for use in other JavaScript code Set a global variable to a map that contains the functions: ```go package main import "" func main() { js.Global.Set("pet", map[string]interface{}{ "New": New, }) } type Pet struct { name string } func New(name string) *js.Object { return js.MakeWrapper(&Pet{name}) } func (p *Pet) Name() string { return } func (p *Pet) SetName(name string) { = name } ``` For more details see [Jason Stone's blog post]( about GopherJS. ### Architecture #### General GopherJS emulates a 32-bit environment. This means that `int`, `uint` and `uintptr` have a precision of 32 bits. However, the explicit 64-bit integer types `int64` and `uint64` are supported. The `GOARCH` value of GopherJS is "js". You may use it as a build constraint: `// +build js,-wasm`. #### Application Lifecycle The `main` function is executed as usual after all `init` functions have run. JavaScript callbacks can also invoke Go functions, even after the `main` function has exited. Therefore the end of the `main` function should not be regarded as the end of the application and does not end the execution of other goroutines. In the browser, calling `os.Exit` (e.g. indirectly by `log.Fatal`) also does not terminate the execution of th
    • html5
      NULL 博文链接:
    • HTML5详解 Html5实战
      HTML5详解 Html5实战 Demos 适合继续充电的开发人员
    • swim.html:http
      swim.html 来源
    • HTML5 知识
      NULL 博文链接:
    • html5_source
      this is for the html5 what is the source
    • Lingulo-Responsive-Tutorial:httpHTML5自适应网站教程
      HTML5网站是Christoph Anastasiades创建HTML5 / CSS3教程的一部分( )随意使用用于您的个人或商业项目和/或以您喜欢的任何方式进行更改。 如果您在网站上的某处放置了指向的链接以支持我的工作,我将很高兴。 ...
    • html5电子签名
    • html5 boilerplate 4.3.0
      html5 boilerplate 4.3.0
    • HTML5-Examples:httpHTML 5示例-html source website
      HTML5-示例 HTML 5示例。 版权所有2012:SKJ Technologies Private Ltd 这些示例已根据“知识共享署名-相同的ShareAlike 3.0未移植”(CC BY-SA 3.0)许可证进行共享。 有关CC BY-SA 3.0许可证的更多详细信息,请...
    • HTML5图片展示