Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GoのWebAssembly活用パターン紹介
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
syumai
June 11, 2025
Programming
13k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoのWebAssembly活用パターン紹介
Go Connect #7 (
https://gotalk.connpass.com/event/355423/
) の発表資料です
syumai
June 11, 2025
More Decks by syumai
See All by syumai
Workers Cache を知る
syumai
0
370
自分的「カンファレンスの楽しみ方」
syumai
0
220
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.6k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
2.2k
Oxlintのカスタムルールの現況
syumai
6
2.2k
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
2
1.7k
『[入門] Cloudflare Workers』本はなぜ誕生したのか
syumai
0
660
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
9
3.4k
知られているようで知られていない JavaScriptの仕様 4選
syumai
3
1.3k
Other Decks in Programming
See All in Programming
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
240
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
1
180
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
160
Kotlinの15年とServer-Side Kotlin
n_takehata
1
160
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
4.3k
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
930
Performance Optimization With LLMs
xleitix
0
190
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
410
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
1k
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
180
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
140
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
247
13k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
570
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
740
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
450
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
580
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
480
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Into the Great Unknown - MozCon
thekraken
41
2.8k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Transcript
Go のWebAssembly 活用パターン紹介 syumai Go Connect #7 (2025/6/11)
自己紹介 syumai ECMAScript 仕様輪読会 / Asakusa.go 主催 株式会社ベースマキナで管理画面のSaaS を開発中 Go
でGraphQL サーバー (gqlgen) や TypeScript でフロント エンドを書いています Software Design 2023 年12 月号から2025 年2 月号まで Cloudflare Workers の連載をしました Twitter ( 現𝕏): @__syumai Website: https://syum.ai
今日話すこと WebAssembly について Go のWebAssembly の特徴 Go のWebAssembly の現在のユースケース Go
のWebAssembly の将来のユースケース
注意 今回の内容は、主にアプリケーション実装者観点での話です WebAssembly 自体の仕様には深く踏み込みません
WebAssembly について
WebAssembly について 主にブラウザを動作環境としたバイナリ形式の言語 バイナリ形式に対応するテキスト形式もある 略称はWasm 最近では、ブラウザ以外の動作環境も増えている
WebAssembly の特徴 ポータブル あらゆるところで動く ブラウザ JS ランタイム (Node.js, Deno, Cloudflare
Workers 等) 各言語向けライブラリ (Wasmtime, Wasmer 等) CLI ツール (Wasmtime, Wasmer 等) Docker (WasmEdge) 安全 サンドボックス化された環境内で実行 素の状態ではOS の機能を触れない
WebAssembly 関連の重要な技術 WebAssembly System Interface 略称はWASI WebAssembly がシステム (OS 等)
とやりとりするためのインタフェース 例えば、以下のようなシステムへの機能呼び出しインタフェースを実装できる ファイルアクセス HTTP 通信
WebAssembly 関連の重要な技術 WebAssembly Component Model WASI 0.2 の一部 WebAssembly 製のコンポーネントが実装するインタフェース定義を自由に行える
インタフェース定義用の言語であるWIT (Wasm Interface Type) が追加 ファイルアクセス、HTTP 通信等のインタフェースもWIT で書かれるように プログラミング言語を跨いだ機能呼び出しが可能に
WIT の例 wit/component.wit package docs:
[email protected]
; interface add { add: func(x:
u32, y: u32) -> u32; } world adder { export add; } Go - The WebAssembly Component Model より http://component-model.bytecodealliance.org/language-support/go.html
Go のWebAssembly の特徴
Go のWebAssembly の特徴 ビルドが簡単 普段のビルドコマンドに GOOS=js GOARCH=wasm を付けるだけでビルド可能 CGO を使っていなければ大体どんな実装でもビルドが通る
バイナリサイズが大きい Go のランタイムのサイズ自体が大きいので、簡単に数MB のバイナリになる 一応TinyGo という道具も使える ( かなり小さくなる) ブラウザ用途では考慮が必要
Go のWebAssembly の現在のユースケース
Go のWebAssembly の現在のユースケース ブラウザでの実行 プラグイン機構の構築 CDN Edge での実行 npm でのコマンド配布
ブラウザでの実行 Go で実装したロジックをブラウザ上のJavaScript から呼び出す Go の充実した標準ライブラリの機能をブラウザで活用可能 暗号関係のライブラリなど DOM 操作などのブラウザAPI の操作用途にはあまり向かない
JS のエコシステムに最適化されているため バイナリサイズが大きいので、トラフィックの多いサイトには向かない
ブラウザでの実行パターン 1. syscall/js.Global() の利用 2. go:wasmexport の利用 3. TinyGo &
Wasm Component の利用
1. syscall/js.Global() の利用 以下のように js.Global().Set("compress", ...) とすると、JS 側からグローバル な compress
関数を呼べる // 受け取ったUint8Arrayをgzip圧縮して返す関数 var compressFunc = js.FuncOf(func(this js.Value, args []js.Value) any { /* 中略 */ r, _ := compressor.Compress(src) var buf bytes.Buffer io.Copy(&buf, r) ua := newUint8Array(buf.Len()) js.CopyBytesToJS(ua, buf.Bytes()) return ua }) func main() { js.Global().Set("compress", compressFunc) select {} } https://github.com/syumai/go-wasm-gzipper/blob/master/main.go
2. go:wasmexport の利用 Go 1.24 でサポート http://go.dev/blog/wasmexport 以下のようにすると、export されたadd 関数をJS
側から呼べる シグニチャに使える型が限られている点が課題 main 関数の select {} のおまじないは不要になった package main //go:wasmexport add func add(x, y int32) int32 { return x + y } func main() {} https://github.com/syumai/til/blob/main/go/wasmexport/main.go
3. TinyGo & Wasm Component の利用 WIT からGo 向けのインタフェースを生成、それを実装してビルドするとWasm Component
化できる Wasm Component はbytecodealliance/jco でTS/JS から利用可能になる 今のところ、TinyGo でしか使えない
3. TinyGo & Wasm Component の利用 WIT の例 package calculator:
[email protected]
;
interface calculator { add: func(a: f64, b: f64) -> f64; subtract: func(a: f64, b: f64) -> f64; /* ... */ } world calculator-world { include wasi:cli/
[email protected]
; export calculator; } https://github.com/syumai/go-wasm- playground/blob/master/components/calculator/wit/world.wit
3. TinyGo & Wasm Component の利用 Go 側の実装例 func init()
{ calculator.Exports.Add = func(a, b float64) float64 { return a + b } calculator.Exports.Subtract = func(a, b float64) float64 { return a - b } /* ... */ } https://github.com/syumai/go-wasm- playground/blob/master/components/calculator/main.go
3. TinyGo & Wasm Component の利用 TS からの利用例 import {
calculator } from '../js-output/calculator.js'; function main() { console.log(calculator.add(1, 2)); // 3 console.log(calculator.subtract(5, 4)); // 1 } https://github.com/syumai/go-wasm- playground/blob/master/components/calculator/src/sample.ts
プラグイン機構の構築 Wasm がポータブルな実行可能バイナ リであることを活用 Go 製のコマンドに、後付けで機能を 追加する github.com/knqyf263/go-plugin が代 表例
図はknqyf263/go-plugin の README から引用 Plugin のインタフェースだけを Protobuf で記述し、その実装は 後から追加できる wazero などで自前構築も可能
CDN Edge での実行 JS / Wasm のみをサポートする、CDN のEdge Function での実行に利用
軽量なランタイムで動作するので、一般的なFaaS と比べてコストが低い傾向にある インフラコストを抑える目的での利用が可能 Fastly Compute はGo をサポートしている (Wasm ランタイム) Cloudflare Workers はgithub.com/syumai/workers などで利用可能 Fermyon のSpin も注目されつつある
CDN Edge での実行 https://syum.ai は実際にTinyGo の Wasm on Cloudflare Workers
で動い ている
npm でのコマンド配布 コマンドの配布チャネルを増やせる 以下は github.com/syumai/cli/xidgen の例
Go のWebAssembly の将来のユースケース
Go のWebAssembly の将来のユースケース 他言語から呼び出すライブラリとしての利用 アプリケーション実行コンテナとしての利用
他言語から呼び出すライブラリとしての利用 jco の例が発展した先の未来 WIT により、実装言語を気にしないライブラリ開発が行えるようになるかもしれない
アプリケーション実行コンテナとしての利用 WASI (WIT) を通じてシステム連携ができるWasm をビルドできれば、Wasm バイナリ 単体をコンテナとして扱える可能性がある WasmEdge によって実証されつつある Docker
image のビルドが不要になって、 go build で出力したWasm をデプロイする だけで済む日がいつか来るかも… ?
まとめ Wasm によって、動作環境に縛られずにGo の機能を活用できる Wasm ビルドの簡単さはGo の大きな特徴 Go を採用する理由の一つとして有力
ご清聴ありがとうございました!