[Golang] GopherJS serve and build Command Usage
GopherJS provide command line tools but there is no enough documentation of the command. I gave the serve and build command a try and give a summary of the usage.
First we see the help of gopherjs command line tool:
$ gopherjs help
GopherJS is a tool for compiling Go source code to JavaScript.
Usage:
gopherjs [command]
Available Commands:
build compile packages and dependencies
get download and install packages and dependencies
install compile and install packages and dependencies
run compile and run Go program
test test packages
tool run specified go tool
serve compile on-the-fly and serve
Flags:
--color colored output (default true)
-m, --minify minify generated code
-q, --quiet suppress non-fatal warnings
--tags string a list of build tags to consider satisfied during the build
-v, --verbose print the names of packages as they are compiled
-w, --watch watch for changes to the source files
Use "gopherjs [command] --help" for more information about a command.
Build Command Usage
We will try build command first. See the help of build command first:
$ gopherjs build --help
compile packages and dependencies
Usage:
gopherjs build [packages] [flags]
Flags:
--color colored output (default true)
-m, --minify minify generated code
-o, --output string output file
-q, --quiet suppress non-fatal warnings
--tags string a list of build tags to consider satisfied during the build
-v, --verbose print the names of packages as they are compiled
-w, --watch watch for changes to the source files
Now I have a Go program named dom.js, put under . directory. Compile this file to JavaScript by:
$ gopherjs build dom.go -o dom.js
The JavaScript output file will be dom.js, put under . directory.
Next we try the -w flag of build command:
$ gopherjs build dom.go -w -o dom.js
The GopherJS command line tool will watch the directory, and if you make any changes of dom.go, it will automatically recompile the Go code to JavaScript. This feature is great for daily development.
Assume GOPATH is set, and you put the dom.go under $(GOPATH)/src/demo directory, you can tell GopherJS command line tool to look for the demo directory by:
$ gopherjs build demo -w -o src/demo/dom.js
The command line tool will compile the dom.go under $(GOPATH)/src/demo and the JavaScript output file will be $(GOPATH)/src/demo/dom.js.
Serve Command Usage
See the help of serve command first:
$ gopherjs serve --help
compile on-the-fly and serve
Usage:
gopherjs serve [flags]
Flags:
--color colored output (default true)
--http string HTTP bind address to serve (default ":8080")
-m, --minify minify generated code
-q, --quiet suppress non-fatal warnings
--tags string a list of build tags to consider satisfied during the build
-v, --verbose print the names of packages as they are compiled
Global Flags:
-w, --watch watch for changes to the source files
Run the command without any flag:
$ gopherjs serve
The GopherJS command line tool will serve $(GOPATH)/src directory by default. It looks like there is no way to change the serving directory. Open your browser at http://localhost:8080 to visit the webpage.
I like to visit the webpage at http://localhost:8000. Change the port by:
$ gopherjs serve --http ":8000"
I try to run the serve command with -w flag but it looks like the command line tool did not watch the changes and recompile for me. So I guess the -w flag is useless combined with serve command.
Tested on: Ubuntu Linux 15.10, Go 1.5.2.
References:
[1] | GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, ) |
[2] | Getting Started with GopherJS |
[3] | GopherJSの紹介 - GolangRdyJp |
[4] | albrow/gopherjs-live · GitHub (Automatic watching and recompiling for gopherjs) |
[5] | ajhager/srvi · GitHub (Quickly build, serve, run, and refresh your GopherJS programs) |
[6] | cmd/gopherjs_serve_html at master · shurcooL/cmd · GitHub |
[7] | Add "gopherjs serve" command · Issue #121 · gopherjs/gopherjs · GitHub |
[8] | It's easy to get an infinite loop with the watch flag · Issue #212 · gopherjs/gopherjs · GitHub |