Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:44:26+00:00 2026-06-18T12:44:26+00:00

I want to be able to fully communicate with some programs after spawning them

  • 0

I want to be able to fully communicate with some programs after spawning them from Golang program. What I already have is spawning process and talking through pipes based on last line read from stdout:

package main

import (
    "fmt"
    "io"
    "log"
    "os/exec"
    "strings"
)

var stdinPipe io.WriteCloser
var stdoutPipe io.ReadCloser
var err error

func main() {
    cmd := &exec.Cmd{
        Path: "/Users/seba/Projects/go/src/bootstrap/in",
        Args: []string{"program"},
    }

    stdinPipe, err = cmd.StdinPipe()

    if err != nil {
        log.Fatal(err)
    }

    stdoutPipe, err = cmd.StdoutPipe()

    if err != nil {
        log.Fatal(err)
    }

    err = cmd.Start()

    if err != nil {
        log.Fatal(err)
    }

    var stdoutLines []string
    go stdoutManage(stdoutLines, stdoutController)

    cmd.Wait()
}

// TODO: imporove as in io.Copy
func stdoutManage(lines []string, manager func(string)) {
    buf := make([]byte, 32*1024)

    for {
        nr, err := stdoutPipe.Read(buf)

        if nr > 0 {
            thelines := strings.Split(string(buf), "\n")
            for _, l := range thelines {
                manager(l)
                lines = append(lines, l)
            }
        }

        buf = make([]byte, 32*1024) // clear buf

        if err != nil {
            break
        }
    }
}

However this approach have problems with programs clearing terminal output and programs which somehow buffer it’s stdin or don’t use stdin at all (don’t know if it’s possible).

So the question: is there a portable way of talking with programs (it can be non-Golang solution)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:44:27+00:00Added an answer on June 18, 2026 at 12:44 pm

    Problems like this are usually to do with the C library which changes its default buffering mode depending on exactly what stdin / stdout / stderr are.

    If stdout is a terminal then buffering is automatically set to line buffered, else it is set to buffered.

    This is relevant to you because when you run the programs through a pipe they aren’t connected to a terminal and so will have buffering which messes up this sort of use.

    To fix, you need to use a pseudo tty which pretends to be a terminal but acts just like a pipe. Here is a library implementing the pty interface which I haven’t actually tried but it looks like it does the right thing!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to take an image that i have already captured
I'm experimenting with Azure Connect. I want to be able to push some files
I have a table with an embedded picture(OLE) coulmn. I Want to be able
I want to be able to program multiple threads with gnu octave so it
Original Question I want to be able to generate a new (fully valid) MP3
I want to be able to transfer values from one DataGridView in Form1 to
I want to be able to return a specific jsp page from restful service
jQuery's deferred's have never liked me, and I've never been fully able to understand
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.