Package Exports
- golikejs
- golikejs/channel
- golikejs/context
- golikejs/sync
Readme
golikejs
golikejs reimplements selected parts of Go's standard library for JavaScript and TypeScript runtimes. The goal is practical API parity where it makes sense, so you can use familiar concurrency and context patterns from Go in JS/TS projects. The project is intentionally small and focused: it reproduces commonly used primitives (sync, context, channels, semaphores, etc.) and keeps semantics close to Go's originals.
Table of contents
- Features
- Installation
- Quick examples
- Mutex
- Channel
- Context
- Cond
- Semaphore
- WaitGroup
- API summary
- Build & testing
- Publishing
- Contributing
- License
Features
- Mutex, RWMutex — mutual exclusion primitives matching Go semantics.
- WaitGroup — wait for a collection of goroutine-like tasks.
- Semaphore — counting semaphore.
- Channel
— unbuffered and buffered channels with send/receive semantics. - select() — multiplex channel operations like Go's select statement.
- Cond — condition variables.
- Context — cancellation propagation and done/error semantics.
Installation
npm install golikejs
# or with bun
bun add golikejsQuick examples
Mutex
import { Mutex } from 'golikejs';
const m = new Mutex();
await m.lock();
try {
// critical section
} finally {
m.unlock();
}Channel (buffered)
import { Channel } from 'golikejs';
const ch = new Channel<number>(3);
await ch.send(1);
const [value, ok] = await ch.receive();Channel select (multiplexing)
import { Channel, select, receive, send, default_ } from 'golikejs';
const ch1 = new Channel<string>();
const ch2 = new Channel<number>();
// Intuitive API with helper functions
let result: string | undefined;
await select([
receive(ch1).then((value, ok) => { result = `ch1: ${value}`; }),
receive(ch2).then((value, ok) => { result = `ch2: ${value}`; }),
send(ch1, 'hello').then(() => { result = 'sent to ch1'; }),
default_(() => { result = 'no data available'; })
]);
// Or using the direct object API
await select([
{ channel: ch1, action: (value, ok) => { result = `ch1: ${value}`; } },
{ channel: ch2, action: (value, ok) => { result = `ch2: ${value}`; } },
{ channel: ch1, value: 'hello', action: () => { result = 'sent to ch1'; } },
{ default: () => { result = 'no data available'; } }
]);Context (cancellation)
import { context } from 'golikejs';
const ctx = context.withCancel(context.Background());
// some async work that listens for cancellation
const task = async (ctx) => {
await ctx.done; // resolves when canceled
};
// cancel the context
ctx.cancel(new Error('shutdown'));Cond
import { Cond, Mutex } from 'golikejs';
const mu = new Mutex();
const cond = new Cond(mu);
// in a waiter
await mu.lock();
try {
await cond.wait();
} finally {
mu.unlock();
}
// elsewhere
await mu.lock();
try {
cond.signal();
} finally {
mu.unlock();
}Semaphore
import { Semaphore } from 'golikejs';
const s = new Semaphore(2);
await s.acquire();
try {
// limited concurrency section
} finally {
s.release();
}WaitGroup
import { WaitGroup } from 'golikejs';
const wg = new WaitGroup();
wg.add(1);
(async () => {
try {
// work
} finally {
wg.done();
}
})();
await wg.wait();API summary
- Mutex:
lock(),unlock(),tryLock() - RWMutex:
rlock(),runlock(),lock(),unlock() - WaitGroup:
add(),done(),wait() - Semaphore:
acquire(),release(),tryAcquire() - Channel
: send(),receive(),trySend(),tryReceive(),close() - select:
select(),receive(),send(),default_() - Cond:
wait(),signal(),broadcast() - Context helpers in
contextmodule:Background(),withCancel(),withTimeout(), andwithDeadline()(see source API for details)
Build & testing
- Run tests with Bun (recommended):
bun test- Build (if needed):
bun run buildPublishing
The repository contains a GitHub Actions workflow that can publish the package to npm when a release is created or a v* tag is pushed. To enable automated publishing:
- Create an npm token (Settings → Access Tokens on npmjs.com) and add it to your GitHub repository secrets as
NPM_TOKEN. - Push a tag such as
v1.0.0or create a GitHub Release. Thepublishworkflow will run and publish to npm with the configured token.
Contributing
Contributions, bug reports, and PRs are welcome. Please open issues for proposals or file PRs with tests where appropriate. We try to keep the APIs stable and faithful to Go where possible.
License
MIT