forked from go-go-golems/go-go-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func main() { | ||
// Reading piped data (if any) | ||
pipedData, _ := ioutil.ReadAll(os.Stdin) | ||
fmt.Printf("Received piped data: %s\n", pipedData) | ||
|
||
// Now, we want to read user input from the terminal | ||
|
||
// Open the terminal for reading | ||
tty, err := os.Open("/dev/tty") | ||
if err != nil { | ||
fmt.Println("Failed to open terminal:", err) | ||
return | ||
} | ||
defer func(tty *os.File) { | ||
_ = tty.Close() | ||
}(tty) | ||
|
||
reader := bufio.NewReader(tty) | ||
|
||
for { | ||
// Ask the user if they want to continue | ||
fmt.Print("Do you want to continue in chat? [y/n]: ") | ||
|
||
input, err := reader.ReadString('\n') | ||
if err != nil { | ||
fmt.Println("Failed to read input:", err) | ||
return | ||
} | ||
|
||
switch input[0] { | ||
case 'y', 'Y': | ||
chat() | ||
case 'n', 'N': | ||
return | ||
default: | ||
fmt.Println("Invalid input. Please enter 'y' or 'n'.") | ||
} | ||
} | ||
} | ||
|
||
func chat() { | ||
fmt.Println("You're now in chat! (For the sake of this example, we'll simply return to the main loop after this message.)") | ||
} |