Movatterモバイル変換


[0]ホーム

URL:


gopherjs

commandmodule
v1.17.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2022 License:BSD-2-ClauseImports:40Imported by:0

Details

Repository

github.com/gopherjs/gopherjs

Links

README

GopherJS - A compiler from Go to JavaScript

GoDocSourcegraphCircle CI

GopherJS compiles Go code (golang.org) 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.

Help us make GopherJS better!

What's new?

  • 2021-09-19: Go 1.17 support is available!
  • 2021-08-23: Go Modules are now fully supported.
  • 2021-06-19: Completesyscall/js package implementation compatible with the upstream Go 1.16.
  • 2021-04-04:Go 1.16 is now officially supported! 🎉 🎉 🎉

Playground

Give GopherJS a try on theGopherJS Playground.

What is supported?

Nearly everything, including Goroutines (compatibility documentation). Performance is quite good in most cases, seeHTML5 game engine benchmark. Cgo is not supported.

Installation and Usage

GopherJSrequires Go 1.17 or newer. If you need an older Goversion, you can use anolder Gopher release.

Get or update GopherJS and dependencies with:

go get -u github.com/gopherjs/gopherjs

If your local Go distribution as reported bygo version is newer than Go 1.17, then you need to set theGOPHERJS_GOROOT environment variable to a directory that contains a Go 1.17 distribution. For example:

go get golang.org/dl/go1.17.1go1.17.1 downloadexport GOPHERJS_GOROOT="$(go1.17.1 env GOROOT)"  # Also add this line to your .profile or equivalent.

Now you can usegopherjs build [package],gopherjs build [files] orgopherjs install [package] which behave similar to thego tool. Formain packages, these commands create a.js file and.js.map source map in the current directory or in$GOPATH/bin. The generated JavaScript file can be used as usual in a website. Usegopherjs 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 defaultGOOS value when generating code. SupportedGOOS values are:linux,darwin. If you're on a different platform (e.g., Windows or FreeBSD), you'll need to set theGOOS 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 usegopherjs run orgopherjs test to run the generated code locally, install Node.js 10.0.0 (or newer), and thesource-map-support module:

npm install --global source-map-support

On supportedGOOS platforms, it's possible to make system calls (file system access, etc.) available. Seedoc/syscalls.md 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 tohttp://localhost:8080/example.com/user/project/ should compile and run the Go packageexample.com/user/project. The generated JavaScript output will be served athttp://localhost:8080/example.com/user/project/project.js (the .js file name will be equal to the base directory name). If the directory containsindex.html it will be served, otherwise a minimalindex.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 rungopherjs serve github.com/user/project then the generated JavaScript for the package github.com/user/project/mypkg will be served athttp://localhost:8080/mypkg/mypkg.js.

Environment Variables

There are some GopherJS-specific environment variables:

  • GOPHERJS_GOROOT - if set, GopherJS uses this value as the default GOROOTvalue, instead of using the system GOROOT as the default GOROOT value
  • GOPHERJS_SKIP_VERSION_CHECK - if set to true, GopherJS will not checkGo version in the GOROOT for compatibility with the GopherJS release. Thisis primarily useful for testing GopherJS against unreleased versions of Go.

Performance Tips

Community

Getting started

Interacting with the DOM

The packagegithub.com/gopherjs/gopherjs/js (seedocumentation) provides functions for interacting with native JavaScript APIs. For example the line

document.write("Hello world!");

would look like this in Go:

js.Global.Get("document").Call("write", "Hello world!")

You may also want use theDOM bindings, thejQuery bindings (seeTodoMVC Example) or theAngularJS bindings. Those are some of thebindings 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:

package mainimport "github.com/gopherjs/gopherjs/js"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 p.name}func (p *Pet) SetName(name string) {p.name = name}

For more details seeJason Stone's blog post about GopherJS.

Architecture

General

GopherJS emulates a 32-bit environment. This means thatint,uint anduintptr have a precision of 32 bits. However, the explicit 64-bit integer typesint64 anduint64 are supported. TheGOARCH value of GopherJS is "js". You may use it as a build constraint:// +build js,-wasm.

Application Lifecycle

Themain function is executed as usual after allinit functions have run. JavaScript callbacks can also invoke Go functions, even after themain function has exited. Therefore the end of themain function should not be regarded as the end of the application and does not end the execution of other goroutines.

In the browser, callingos.Exit (e.g. indirectly bylog.Fatal) also does not terminate the execution of the program. For convenience, it callsruntime.Goexit to immediately terminate the calling goroutine.

Goroutines

Goroutines are fully supported by GopherJS. The only restriction is that you need to start a new goroutine if you want to use blocking code called from external #"https://github.com/gopherjs/gopherjs/wiki/Developer-Guidelines" rel="nofollow">Developer Guidelines for additional developer information.

Documentation

The Go Gopher

There is no documentation for this package.

Source Files

View all Source files

Directories

PathSynopsis
Package build implements GopherJS build system.
Package build implements GopherJS build system.
cache
Package cache solves one of the hardest computer science problems in application to GopherJS compiler outputs.
Package cache solves one of the hardest computer science problems in application to GopherJS compiler outputs.
versionhack
Package versionhack makes sure go/build doesn't disable module support whenever GopherJS is compiled by a different Go version than it's targeted Go version.
Package versionhack makes sure go/build doesn't disable module support whenever GopherJS is compiled by a different Go version than it's targeted Go version.
Package compiler implements GopherJS compiler logic.
Package compiler implements GopherJS compiler logic.
gopherjspkg
Package gopherjspkg provides core GopherJS packages via a virtual filesystem.
Package gopherjspkg provides core GopherJS packages via a virtual filesystem.
natives
Package natives provides native packages via a virtual filesystem.
Package natives provides native packages via a virtual filesystem.
internal
srctesting
Package srctesting contains common helpers for unit testing source code analysis and transformation.
Package srctesting contains common helpers for unit testing source code analysis and transformation.
sysutil
Package sysutil contains system-specific utilities.
Package sysutil contains system-specific utilities.
Package js provides functions for interacting with native JavaScript APIs.
Package js provides functions for interacting with native JavaScript APIs.
Package tests contains tests for GopherJS.
Package tests contains tests for GopherJS.
maincommand

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp