reading line by line

This commit is contained in:
potassium 2023-10-18 11:20:47 +03:00
parent fd6c5d76c4
commit e35b0158d7

33
main.go
View File

@ -2,6 +2,7 @@
package main
import (
"bufio"
"io"
"os"
@ -14,28 +15,42 @@ import (
func main() {
english := flag.Bool("en", false, "translate from english")
var source, into string = "ru", "en"
var text interface{}
var text string
var err error
flag.Parse()
if *english {
source, into = into, source
}
Args := flag.Args()
if len(Args) > 0 {
text = strings.Join(Args[:], " ")
} else {
text, err = io.ReadAll(os.Stdin)
r := bufio.NewReader(os.Stdin)
for {
text, err = r.ReadString('\n')
if err != nil {
switch err {
case io.EOF:
return
default:
panic(err)
}
}
if len(text) == 0 {
os.Exit(1)
}
result, err := gt.Translate(text, source, into)
if err != nil {
panic(err)
}
if len(text.([]byte)) == 0 {
os.Exit(1)
fmt.Println(result)
}
text = string(text.([]byte)) // looks awful but it works
}
if *english {
source, into = into, source
}
result, err := gt.Translate(text.(string), source, into)
result, err := gt.Translate(text, source, into)
if err != nil {
panic(err)
}