dsfdsf8888 2014-12-27 14:25
浏览 914
已采纳

golang读取多行输入的文本并停止ctrl + s

How can I read more than one line in golang, console input.

reader := bufio.NewReader(os.Stdin)

fmt.Println("Enter Text:")

text, _ := reader.ReadString('
')

How I must change ReadString(???) to stop reading if the user hit: <kbd>ctrl</kbd>+<kbd>s</kbd> The New Line ( ) must remain a constituent of the input.

Is there some library/framework for that?

Thanks for every help.

Cheers

  • 写回答

1条回答 默认 最新

  • drblhw5731 2014-12-27 15:45
    关注

    Here is an answer for Linux (and could work for other *nix platforms)

    Ctrl+S is a control character DC3, it means "XOFF, stop sending". If you press Ctrl+S the terminal would freeze.

    Ctrl+Q is a control character DC1, it mean "XON, continue sending", it is necessary after Ctrl+S to unfreeze.

    To use the Ctrl+S, first disable XON/XOFF with stty -ixon (maybe in in startup script) After disabling XON/XOFF, the sample code is following (DC3 is HEX:13/DEC:19 in ASCII table);

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
    )
    
    func main() {
        reader := bufio.NewReader(os.Stdin)
        fmt.Println("Enter Text:")
        text, _ := reader.ReadString('\x13')
        fmt.Println(text)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程