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
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
300
自分的「カンファレンスの楽しみ方」
syumai
0
210
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.5k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.9k
Oxlintのカスタムルールの現況
syumai
6
2.1k
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
2
1.6k
『[入門] Cloudflare Workers』本はなぜ誕生したのか
syumai
0
610
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
9
3.4k
知られているようで知られていない JavaScriptの仕様 4選
syumai
3
1.3k
Other Decks in Programming
See All in Programming
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
500
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
250
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
350
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
440
カツオ、ご期待ください
suneo3476
0
110
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.4k
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
140
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
310
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
550
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
420
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
390
Featured
See All Featured
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
840
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
680
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
700
RailsConf 2023
tenderlove
30
1.6k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
350
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
410
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
250
Chasing Engaging Ingredients in Design
codingconduct
0
340
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
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 を採用する理由の一つとして有力
ご清聴ありがとうございました!