Go (Golang), is an open-source, statically typed, and compiled programming language developed by Google. It is designed for simplicity, efficiency, and strong support for concurrency, making it well-suited for building scalable and high-performance applications.
LiveCodes uses Yaegi, the Go interpreter (running on WebAssembly), to run Go in the browser.
LiveCodes also supports running Go using GopherJS which is a Go to JavaScript compiler. Read documentation here.
Demo
show code
import { createPlayground } from 'livecodes';
const options = {
"params": {
"go-wasm": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n",
"console": "full"
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"params": {
"go-wasm": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n",
"console": "full"
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"params": {
"go-wasm": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n",
"console": "full"
}
};
return (<LiveCodes {...options}></LiveCodes>);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"params": {
"go-wasm": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n",
"console": "full"
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
let options = $state({
"params": {
"go-wasm": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n",
"console": "full"
}
});
let container = $state(null);
onMount(() => {
createPlayground(container, options);
});
</script>
<div bind:this="{container}"></div>
Usage
LiveCodes runs Go in the browser, including the standard library.
Communication with JavaScript
The Go code runs in the context of the result page.
A few helper properties and methods are available in the browser global livecodes.goWasm object:
livecodes.goWasm.input: the initial standard input that is passed to the Go code.
livecodes.goWasm.loaded: A promise that resolves when the Go environment is loaded. Any other helpers should be used after this promise resolves.
livecodes.goWasm.output: the standard output.
livecodes.goWasm.error: the standard error.
livecodes.goWasm.exitCode: the exit code.
livecodes.goWasm.run: a function that runs the Go code with new input. This function takes a string as input and returns a promise that resolves when the Go code is done running. The promise resolves with an object containing the input, output, error, and exitCode properties.
See the example below for more details.
Language Info
Name
go-wasm
Extensions
wasm.go, go-wasm, gowasm
Editor
script
Compiler
Yaegi, compiled to WebAssembly (yaegi-wasm)
Version
Yaegi v0.16.1, running go1.25.0
Using GopherJS
Example Usage
This example demonstrates standard library usage and JavaScript interoperability (also check the code in the HTML editor):
show code
import { createPlayground } from 'livecodes';
const options = {
"template": "go-wasm"
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"template": "go-wasm"
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"template": "go-wasm"
};
return (<LiveCodes {...options}></LiveCodes>);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"template": "go-wasm"
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
let options = $state({
"template": "go-wasm"
});
let container = $state(null);
onMount(() => {
createPlayground(container, options);
});
</script>
<div bind:this="{container}"></div>
Live Reload
By default, new code changes are sent to the result page for re-evaluation without a full page reload, to avoid the need to reload the Go environment.
This behavior can be disabled by adding the code comment // __livecodes_reload__ to the code, which will force a full page reload.
This comment can be added in the hiddenContent property of the editor for embedded playgrounds.
Starter Template
https://livecodes.io/?template=go-wasm
Links