SlideShare a Scribd company logo
David Chang
DevOps@mithril
初探 Go-WebAssembly
What is WebAssembly
WebAssembly
● A new language for web
● Compiled from other
languages
● Offers maximized, reliable
performance
● Not replacing JS
A new language for web
● A low level language
● standard
● a binary instruction and a
assembly-like format
● Supported by Mozilla, Google,
Microsoft, Apple
WebAssembly Now
● Release 1.0
(Draft, last updated Dec 13,
2018)
● Experimental
Why WebAssembly
What’s wrong with JS?
Web Javascript
● Slow
● Insecure
● Dynamic type vs static type
● Avoid heavy computing in
Front-End
● Easy-writing
● Huge eco-system
Historical Javascript
● ECMAScript
-> by Brendan Eich in late 1995
● Internet Explorer 3
● NodeJS -> 2009
● npm -> 2010
JS is getting faster
● Just In Time compiler of JS
engine
● trace and re-optimize (loop)
● GC
● Built-in functions / Stadard
Library / APIs
But wasm is faster
https://hacks.mozilla.org/2017/02/w
hat-makes-webassembly-fast/
● Wasm is compiled
● No type assertion
● No need to re-optimize
● Much less GC
.go
.wasm
(IR)
Firefox x86
Assembly
spider
monkey
Chart Data Source Info
From WebAssembly to JS engine
https://mbebenita.github.io/WasmExplorer/
Some Examples
“Unleash the power of your web devices”
Wasm vs JS
github.com/shamadee/web-dsp
https://d2jta7o2zej4pf.cloudfront.net/
Epic Epic Zen Garden
Unreal Engine in WebAssembly and WebGL 2.0
https://s3.amazonaws.com/mozilla-games/ZenGarden/EpicZenGarden.html
Go WebAssembly
From Go to web
Go-WebAssembly
● go 1.11 (Aug. 2018)
● Experimental feature
https://github.com/golang/go/wiki/W
ebAssembly
1.11 Release Note
Go 1.11 adds an experimental port to WebAssembly (js/wasm).
Go programs compile to one WebAssembly module
Go runtime for goroutine scheduling, garbage collection,
maps, etc.
Go programs can call into JavaScript using the new
experimental syscall/js package.
new GOOS "js" and GOARCH "wasm"
Why Go-WebAssembly
Again, why?
Go-WebAssembly
● Runs existing program / library
in Front-End
● Make golang protable
● Thread Safe
● Back-End to Full-Stack?
Let’s Go-WebAssembly
<html>
<head>
<meta charset="utf-8">
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(
fetch("main.wasm"),
go.importObject)
.then((result) => {
go.run(result.instance);
});
</script>
</head>
<body></body>
</html>
run-%:
GOOS=js GOARCH=wasm
go run -exec="$(shell go env
GOROOT)/misc/wasm/go_js_wasm_exec" ./src/$*
test:
GOOS=js GOARCH=wasm
go test -exec="$(shell go env GOROOT)/misc/wasm/go_js_wasm_exec" ./...
build-%:
GOOS=js GOARCH=wasm
go build -o public/lib.wasm ./src/$*
server:
go run ./src/server -listen :8080 -dir public
.go .wasm JS API
Engine
Chart Data Source Info
From Golang to Web
Compile golang functions to wasm, and runs in JS
package main
import (
"strconv"
"syscall/js"
)
func add(i []js.Value) {
doc := js.Global().Get("document")
value1 := doc.Call("getElementById", "input1").Get("value").String()
value2 := doc.Call("getElementById", "input2").Get("value").String()
int1, _ := strconv.Atoi(value1)
int2, _ := strconv.Atoi(value2)
doc.Call("getElementById", "sum").Set("value", int1+int2)
}
// JS
func add() {
var value1 = document.getElementById("input1").value;
vat value2 = document.getElementById("input2").value;
document.getElementById("sum").value = Number(value1) + Number(value2);
}
// Go
func add(i []js.Value) {
doc := js.Global().Get("document")
value1 := doc.Call("getElementById", "input1").Get("value").String()
value2 := doc.Call("getElementById", "input2").Get("value").String()
int1, _ := strconv.Atoi(value1)
int2, _ := strconv.Atoi(value2)
doc.Call("getElementById", "sum").Set("value", int1+int2)
}
Pacakge syscall/js
● Bumpy when manipulate DOM
● access WebAssembly host
environment using the
js/wasm architecture
● API is based on JavaScript
semantics.
● EXPERIMENTAL
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
println("Callbacks registered.") // console.log
}
func registerCallbacks() {
js.Global().Set("add", js.NewCallback(add))
js.Global().Set("subtract", js.NewCallback(subtract))
}
Use Go as Modules
● Import Go packages
● Complie to .wasm
● Register functions as JS
callbacks
● Invoke functions in JS
● Leave the DOMs to JS
Benefits from Go-wasm
● Runs existing program / library
in Front-End
● Make golang protable
● Thread Safe
● Join .wasm from other
languages
Go-Wasm Examples
https://justinclift.github.io/wasmGraph1/
GameBoyColor-Wasm
https://github.com/djhworld/gomeboycolor-wasm
Move Back-End Front
Performance no longer a problem. What you want to run in browser?
kubernetes, kubectl, geth...
Lin Clark: A Cartoon Intro to WebAssembly | JSConf EU 2017
https://www.youtube.com/watch?v=HktWin_LPf4
https://hacks.mozilla.org/2017/02/a-cartoon-intro-to-webassembly/
Dan Callahan: Practical WebAssembly | JSConf Budapest 2017
https://www.youtube.com/watch?v=bac0dGQbUto
Using WebAssembly and Threads (Chrome Dev Summit 2018)
https://www.youtube.com/watch?v=zgOGZgAPUjQ
Watch Lin Clark cartoon if you only have 30 mins!
Q&A

More Related Content

PDF
Production Ready Javascript With Grunt
PDF
Preprocessor Workflow with Grunt
PPTX
Grunt to automate JS build
PDF
Front-end development automation with Grunt
PDF
Grunt.js and Yeoman, Continous Integration
PDF
Webpack DevTalk
PDF
Introduction to Express and Grunt
PDF
Bower & Grunt - A practical workflow
Production Ready Javascript With Grunt
Preprocessor Workflow with Grunt
Grunt to automate JS build
Front-end development automation with Grunt
Grunt.js and Yeoman, Continous Integration
Webpack DevTalk
Introduction to Express and Grunt
Bower & Grunt - A practical workflow

What's hot (20)

PDF
Hey webpack
PPTX
Create Rest API in Nodejs
PDF
Webpack: from 0 to 2
PPTX
Improving build solutions dependency management with webpack
PDF
Webpack
PPTX
JS & NodeJS - An Introduction
PDF
Node.js with Express
PDF
Bundle your modules with Webpack
PDF
Webpack: your final module bundler
PDF
Create a RESTful API with NodeJS, Express and MongoDB
PPTX
CasperJS
PDF
Continuous Integration for front-end JavaScript
KEY
Nodejs web,db,hosting
PPTX
Grunt - The JavaScript Task Runner
PPTX
An Intro into webpack
PDF
JavaScript Engine and WebAssembly
PPTX
Node.js 201: building real-world applications in pure JavaScript
ODP
What grunt?
PDF
Advanced front-end automation with npm scripts
Hey webpack
Create Rest API in Nodejs
Webpack: from 0 to 2
Improving build solutions dependency management with webpack
Webpack
JS & NodeJS - An Introduction
Node.js with Express
Bundle your modules with Webpack
Webpack: your final module bundler
Create a RESTful API with NodeJS, Express and MongoDB
CasperJS
Continuous Integration for front-end JavaScript
Nodejs web,db,hosting
Grunt - The JavaScript Task Runner
An Intro into webpack
JavaScript Engine and WebAssembly
Node.js 201: building real-world applications in pure JavaScript
What grunt?
Advanced front-end automation with npm scripts
Ad

Similar to Intro to go web assembly (20)

PDF
Meetup Performance
PDF
Meetup Performance
PDF
Javascript ui for rest services
PDF
New Features Coming in Browsers (RIT '09)
PDF
Grunt & Front-end Workflow
PDF
Let Grunt do the work, focus on the fun!
PDF
Developing High Performance Web Apps
PDF
Grails 101
PDF
Why Gradle?
PDF
Cannibalising The Google App Engine
PDF
Module, AMD, RequireJS
PDF
Devfest09 Cschalk Gwt
PDF
Supercharging tutorials with WebAssembly
PDF
The Dojo Build System
PPTX
7 tips for javascript rich ajax websites
PDF
Treinamento frontend
PDF
Javascript as a target language - GWT KickOff - Part 2/2
PPTX
Andriy Shalaenko - GO security tips
PPTX
Javascript first-class citizenery
PDF
Cape Cod Web Technology Meetup - 2
Meetup Performance
Meetup Performance
Javascript ui for rest services
New Features Coming in Browsers (RIT '09)
Grunt & Front-end Workflow
Let Grunt do the work, focus on the fun!
Developing High Performance Web Apps
Grails 101
Why Gradle?
Cannibalising The Google App Engine
Module, AMD, RequireJS
Devfest09 Cschalk Gwt
Supercharging tutorials with WebAssembly
The Dojo Build System
7 tips for javascript rich ajax websites
Treinamento frontend
Javascript as a target language - GWT KickOff - Part 2/2
Andriy Shalaenko - GO security tips
Javascript first-class citizenery
Cape Cod Web Technology Meetup - 2
Ad

More from Che-Chia Chang (8)

PDF
COSCUP Scouter: Face recognizer retrieves your Github contribution
PDF
Elk for applications on k8s
PDF
Gdg devfest-2018
PDF
CRI, OCI, and CRI-O
PPTX
Kubernetes networks
PPTX
Automated container-deployment-on-kubernetes
PPTX
Deploy High Availability Kubernetes with Kubespray
PDF
K8s storage-glusterfs-20180210
COSCUP Scouter: Face recognizer retrieves your Github contribution
Elk for applications on k8s
Gdg devfest-2018
CRI, OCI, and CRI-O
Kubernetes networks
Automated container-deployment-on-kubernetes
Deploy High Availability Kubernetes with Kubespray
K8s storage-glusterfs-20180210

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
history of c programming in notes for students .pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Introduction to Artificial Intelligence
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administraation Chapter 3
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Softaken Excel to vCard Converter Software.pdf
Reimagine Home Health with the Power of Agentic AI​
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
history of c programming in notes for students .pptx
Computer Software and OS of computer science of grade 11.pptx
Introduction to Artificial Intelligence
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
Wondershare Filmora 15 Crack With Activation Key [2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administraation Chapter 3
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf

Intro to go web assembly

Editor's Notes

  • #6: Runs in javascript engines
  • #8: Slow: type assertion, parse, execute, requires lots of optimization Insecure: memory control
  • #9: Designed for web
  • #10: Slow: type assertion, parse, execute, requires lots of optimization
  • #11: Near machine code performance
  • #17: Runs in javascript engines
  • #20: Go
  • #22: The WebAssembly.instantiateStreaming() function compiles and instantiates a WebAssembly module directly from a streamed underlying source. This is the most efficient, optimized way to load wasm code.
  • #27: Go