Cross-platform IPC library implemented in pure Go

Project address runoneall/pgoipc: A cross‑platform IPC library implemented in pure Go, including server and client

This package supports Unix (Domain Socket) and Windows (Named Pipe)

The package re‑wraps net and github.com/Microsoft/go-winio, so you don’t have to consider server startup and client connection logic. By exposing conn net.Conn for operating on the connection, it provides the same API across different platforms.

In addition to the README, I also wrote a small demo that transfers Stdin Stdout Stderr via IPC

package main

import (
	"fmt	"io"
	"net"
	"os"
	"os/exec"
	"time"

	"github.com/runoneall/pgoipc/client"
	"github.com/runoneall/pgoipc/server"
)

func startShell(conn net.Conn) {
	proc := exec.Command("sh")

	proc.Stdin = conn
	proc.Stdout = conn
	proc.Stderr = conn

	fmt.Println(proc.Run())
}

func connectShell(conn net.Conn) {
	go io.Copy(conn, os.Stdin)
	io.Copy(os.Stdout, conn)
}

func main() {
	go server.Serv("ipcshell", startShell)

	time.Sleep(time.Second)

	client.Connect("ipcshell", connectShell)
}

6 Likes

OP is awesome

2 Likes

Thank you for sharing

2 Likes

Big boss Burdock :+1:

2 Likes

Support the OP

2 Likes

Learned, thanks for sharing,

What is it used for?

2 Likes

Interprocess data transfer

Oh! Thanks for the answer :folded_hands:

2 Likes

I don’t get it, it looks impressive :yawning_face:

2 Likes

Although I don’t understand it, it feels impressive.

Thanks for sharing
:xhj35:

2 Likes

Take a look

That’s impressive, can you neofetch?

Either works, IPC stands for inter-process communication; any data that can be encoded into bytes can be transmitted.

Simple Explanation of IPC Communication and Process Control

1. What is a Process?

Think of a process as a running program:

You double-click an icon → the operating system starts a process
For example:
- Opening WeChat = one process
- Opening a browser = one process
- Running this Go program = one process

Key characteristics:

  • Each process has its own independent memory space (does not interfere with others)
  • Like each person lives in their own house

2. What is IPC (Inter-Process Communication)?

IPC = Inter-Process Communication = passing messages between different processes

Everyday analogy

Case 1: Two people in the same room talking
→ This is thread communication (very simple, direct talking)

Case 2: People in two different houses communicating
→ This is process communication (requires phone calls, letters, etc.)

Common IPC Methods

1. Pipe (Pipe)
   Like a pipe connecting two houses, can transmit items

2. Unix Socket / Named Pipe
   Like digging a mailbox on the ground, everyone can drop letters into it

3. Network Socket (TCP/UDP)
   Like making a phone call, can communicate across cities

4. Shared Memory
   Like two families sharing a warehouse

3. What does this code do?

Using phone calls as an analogy:

┌──────────────────────────────────────┐
│         Your Go program                │
├──────────────────────────────────────┤
│                                      │
│  [Server]                           │
│   Like a call center, receiving calls│
│   - Open a hotline "ipcshell"        │
│   - When someone calls, start a shell│
│                                      │
│           ↕  (IPC channel)           │
│                                      │
│  [Client]                           │
│   Like the person making the call    │
│   - Dial "ipcshell"                  │
│   - Send your keyboard input         │
│   - Display the other side's reply on the screen│
│                                      │
└──────────────────────────────────────┘

Detailed Process

# Step 1: Start the server
go server.Serv("ipcshell", startShell)
→ Opened a hotline named "ipcshell"
→ When someone connects, execute the startShell function

# Step 2: Client connects
client.Connect("ipcshell")
→ Dial the "ipcshell" hotline

# Step 3: Server responds
startShell() executes:
→ Launch a shell program (like Windows cmd)
→ The shell's input and output are transmitted through this "phone line"

# Step 4: Interaction
You input: ls
→ Sent to the shell via IPC
→ Shell executes
→ Result returns via IPC
→ Displayed on your screen

4. What is Process Control?

Process control = controlling the start, I/O, and termination of other programs

Process control in the code

proc := exec.Command("sh")  // ← Prepare to start the sh program

proc.Stdin = conn   // ← Control its input (the content you type)
proc.Stdout = conn  // ← Control its output (command execution results)
proc.Stderr = conn  // ← Control its error messages

proc.Run()          // ← Start and wait for it to finish

Everyday analogy

You (the main program) hired an assistant (shell process):

You speak → Assistant hears (Stdin)
Assistant replies → You hear (Stdout)
Assistant errors → You also hear (Stderr)

You fully control the assistant's "ears" and "mouth"

5. Full Scenario Demonstration

Suppose you run this program:

$ go run main.go

# What happens inside the program:

[Timeline]
0s  → Start server goroutine, listening on "ipcshell"
1s  → Start client, connecting to "ipcshell"
     → Server accepts connection, starts sh process
     → Establish communication channel

# Now you can input commands
You input > ls
       ↓ (via IPC)
       ↓
    [sh process executes]
       ↓
    file1.txt
    file2.go
       ↓ (via IPC)
       ↓
Display > file1.txt
     file2.go

6. Why do this?

Can’t we just run the shell directly?

// Direct method
exec.Command("sh").Run()  
// Problem: you cannot see or control the input/output

// IPC method
// Advantage: can intercept, log, and forward all content

Real-world applications

. Remote control

Your computer (client) 
    ↓ IPC/network
Server (starts shell)

2. Docker containers

docker exec -it <container> /bin/bash
↑ Actually this is IPC communication + process control

3. Secure sandbox

Main program (secure environment)
    ↓ IPC
Dangerous program (isolated environment, even if it crashes it does not affect the main program)

7. Simple Analogy Summary

Concept Everyday analogy
Process A person who is working
IPC A telephone line between two people
Process control You hire and manage an assistant
Stdin Assistant’s ears
Stdout Assistant’s mouth
This code You remotely control an assistant via phone to do work

Hope this explanation is clearer! :blush:

I looked it up specifically, and I also studied the basics.

6 Likes

Support sharing. It’s a professional niche, I guess.

Even though I don’t understand, it doesn’t hinder my learning.

Support it

Learned