如何在CLI应用程序(golang)中执行自动完成命令?

问题描述:

I want to do autocomplete my commands when press "TAB" key "\t" and execute command when press "ENTER" key "? ". This code for example:

package main
import (
    "bufio"
    "fmt"
)
func main() {
    reader := bufio.NewReader(os.Stdin)
    command, err := reader.ReadString('
')
    if err != nil {
         fmt.Printf("%s
", err)
    } else {
        fmt.Printf("%s
", command)
    }
}

我想在按“ TAB”键“ \ t”时自动完成命令,并在按“ ENTER”时执行命令 “键” ? “。 例如,以下代码: p>

  package main 
import(
“ bufio” 
“ fmt” 
)
func main(){
 reader:= bufio。  NewReader(os.Stdin)
命令,err:= reader.ReadString('
')
如果err!= nil {
 fmt.Printf(“%s 
”,err)
} else {  
 fmt.Printf(“%s 
”,命令)
} 
} 
  code>  pre> 
  div>

If you want bash-like functionality, then it's a lot more complicated than just using a bufio reader.

Check https://github.com/chzyer/readline