Harish
Harish

Reputation: 41

golang attach a socket to Stdin/Stdout

What is the best way to attach a socket to Stdin/Stdout. I know we can redirect the stdin/Stdout to any any file descriptor but how can we do the same with sockets. (like how socat works) ?

Upvotes: 2

Views: 2186

Answers (1)

Endophage
Endophage

Reputation: 21473

Well the socket types in Go implement the io.Writer interface, and os.Stdin implements the io.Reader, so my first guess would be to try out bufio.Writer. It would probably look something like:

package main

import (
    "bufio"
    "os"
)

func main() {
    socket := getSocket() // left as an exercise for you to implement
    writer := bufio.NewWriter(socket)
    writer.ReadFrom(os.Stdin)
    // do something to determine when to stop
}

Upvotes: 2

Related Questions