A comprehensive TypeScript library that brings Go's powerful concurrency primitives to Node.js with true parallelism using Worker Threads, enabling robust, scalable concurrent programming patterns with multi-core performance.
npm install gonex
This package is available on npm as gonex.
Install with:
npm install gonex
# or
yarn add gonex
# or
pnpm add gonex
import { go, channel, select, waitGroup } from 'gonex';
// Simple goroutine (event-loop)
go(async () => {
console.log('Hello from goroutine!');
});
// Channel communication
const ch = channel<string>();
go(async () => {
await ch.send('Hello from sender!');
});
go(async () => {
const msg = await ch.receive();
console.log(msg); // "Hello from sender!"
});
// WaitGroup synchronization
const wg = waitGroup();
for (let i = 0; i < 3; i++) {
wg.add(1);
go(async () => {
await sleep(1000);
console.log('Worker completed');
wg.done();
});
}
await wg.wait();
console.log('All workers completed');
await shutdownParallelScheduler();
import { initializeParallelScheduler, shutdownParallelScheduler } from 'gonex';
await initializeParallelScheduler({ useWorkerThreads: true });
const result = await go(
async () => {
const fs = require('node:fs');
const crypto = require('node:crypto');
const moment = (await import('moment')).default;
const moment_res = {
version: moment.version,
now: moment().format(),
isValid: moment().isValid(),
};
return {
fileExists: fs.existsSync('/tmp/test'),
randomBytes: crypto.randomBytes(4).toString('hex'),
...moment_res,
};
},
[],
{ useWorkerThreads: true }
);
import { go, goAll } from 'gonex';
// CPU-intensive tasks run in parallel across multiple cores
const heavyTasks = [
() => {
let result = 0;
for (let i = 0; i < 1000000000; i++) {
result += Math.sqrt(i);
}
return result;
},
() => {
let result = 0;
for (let i = 0; i < 1000000000; i++) {
result += Math.pow(i, 2);
}
return result;
},
];
// Execute in parallel using worker threads
const results = await goAll(heavyTasks, [], { useWorkerThreads: true });
console.log('All tasks completed in parallel!');
import { go, goAll } from 'gonex';
// Event-loop execution (single-threaded)
const eventLoopResults = await goAll(tasks, [], { useWorkerThreads: false });
// Execution time: ~16 seconds
// Worker thread execution (multi-core)
const parallelResults = await goAll(tasks, [], { useWorkerThreads: true });
// Execution time: ~6 seconds (2.6x faster!)
📖 Full API Documentation: https://thutasann.github.io/gonex/
This package is built with enterprise-level architecture principles:
This project uses automated CI/CD with GitHub Actions:
developnpm testdevelopReleases are managed through GitHub Actions:
develop to masterMIT License - see LICENSE file for details.