0

I want to create a simple program using Go that can get an output in the terminal output. For example:

echo "john" | goprogram

The output is hi john

When using command cat

cat list_name.txt | goprogram

The output using

hi doe
hi james
hi chris

Is there a way to do this using Go?

2

1 Answer 1

1

Read from os.Stdin. Here's an example implementation of the Hi program.

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {
    s := bufio.NewScanner(os.Stdin)
    for s.Scan() {
        fmt.Println("hi", s.Text())
    }
    if s.Err() != nil {
        log.Fatal(s.Err())
    }
}

This program creates a scanner to read os.Stdin by line. For each line in stdin, the program prints "hi" and the line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.