aini 发表于 2018-9-20 08:37:25

Golang的交互模式进阶

  Golang的交互模式进阶-读取用户的输入
  作者:尹正杰
  版权声明:原创作品,谢绝转载!否则将追究法律责任。
  读写数据除了 fmt 和 os 包,我们还需要用到 bufio 包来处理缓冲的输入和输出。
  一.从控制台读取输入
  我们如何读取用户的键盘(控制台)输入呢?从键盘和标准输入 os.Stdin 读取输入,最简单的办法是使用 fmt 包提供的 Scan 和 Sscan 开头的函数。具体代码如下
  

/*  
#!/usr/bin/env gorun
  
@author :yinzhengjie
  
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
  
EMAIL:y1053419035@qq.com
  
*/
  

  
package main
  
import "fmt"
  
var (
  
   FirstName, SecondNames, ThirdNames string
  
   i int
  
   f float32
  
   Input = "5.2 / 100 / Golang"      //用户自定义变量,便于之后对这个字符串的处理。
  
   format = "%f / %d / %s"
  
)
  
func main() {
  
   fmt.Printf("Please enter your full name: ")
  
   fmt.Scanln(&FirstName, &SecondNames)    //Scanln 扫描来自标准输入的文本,将空格分隔的值依次存放到后续的参数内,直到碰到换行。
  
   // fmt.Scanf("%s %s", &firstName, &lastName)    //Scanf与其类似,除了 Scanf 的第一个参数用作格式字符串,用来决定如何读取。
  

  
   fmt.Printf("Hi %s %s!\n", FirstName, SecondNames)
  
   fmt.Sscanf(Input, format, &f, &i, &ThirdNames)    //Sscan 和以 Sscan 开头的函数则是从字符串读取,除此之外,与 Scanf 相同。如果这些函数读取到的结果与您预想的不同,您可以检查成功读入数据的个数和返回的错误。
  

  
   fmt.Println("From the Input we read: ", f, i, ThirdNames)
  
}
  

  

  

  
#以上代码执行结果如下:
  
Please enter your full name: yinzhengjie
  
Hi yinzhengjie !
  
From the Input we read:5.2 100 Golang
  

  二.从缓冲读取输入
  bufio.NewReader() 构造函数的签名为: func NewReader(rd io.Reader) *Reader。该函数的实参可以是满足 io.Reader 接口的任意对象,函数返回一个新的带缓冲的 io.Reader 对象,它将从指定读取器(例如 os.Stdin )读取内容。返回的读取器对象提供一个方法 ReadString(delim byte) ,该方法从输入中读取内容,直到碰到 delim指定的字符,然后将读取到的内容连同 delim 字符一起放到缓冲区。ReadString 返回读取到的字符串,如果碰到错误则返回 nil 。如果它一直读到文件结束,则返回读取到的字符串和 io.EOF 。如果读取过程中没有碰到 delim 字符,将返回错误 err != nil 。
  

/*  
#!/usr/bin/env gorun
  
@author :yinzhengjie
  
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
  
EMAIL:y1053419035@qq.com
  
*/
  

  
package main
  

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

  
var (
  
   inputReader *bufio.Reader //inputReader 是一个指向 bufio.Reader 的指针。
  
   input string
  
   err error
  
)
  
func main() {
  
   inputReader = bufio.NewReader(os.Stdin)    //创建一个读取器,并将其与标准输入绑定。
  
   fmt.Printf("Please enter some input: ")
  
   input, err = inputReader.ReadString('\n') //读取器对象提供一个方法 ReadString(delim byte) ,该方法从输入中读取内容,直到碰到 delim 指定的字符,然后将读取到的内容连同 delim 字符一起放到缓冲区。
  
   if err == nil {
  
         fmt.Printf("The input was: %s", input)
  
   }
  
}
  

  

  

  
#以上代码执行结果如下:
  
Please enter some input: yinzhengjie
  
The input was: yinzhengjie
  

  三.从键盘读取输入
  使用了 switch 语句来判断用户输入的字符串。
  

/*  
#!/usr/bin/env gorun
  
@author :yinzhengjie
  
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
  
EMAIL:y1053419035@qq.com
  
*/
  

  
package main
  

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

  
func main() {
  
   inputReader := bufio.NewReader(os.Stdin)
  
   fmt.Printf("Please enter your name:")
  
   input, err := inputReader.ReadString('\n')
  
   if err != nil {
  
         fmt.Println("There were errors reading, exiting program.")
  
         return
  
   }
  
   fmt.Printf("Your name is %s", input)
  
   switch input {
  
   case "yinzhengjie\n":
  
         fmt.Println("Welcome yinzhengjie!")
  
   case "bingan\n":
  
         fmt.Println("Welcome bingan!")
  
   case "liufei\n":
  
         fmt.Println("Welcome liufei")
  
   default:
  
         fmt.Println("You are not welcome here! Goodbye!")
  
   }
  
/*    //version 2:
  
   switch input {
  
   case "yinzhengjie\n":
  
         fallthrough
  
   case "jiashanpeng\n":
  
         fallthrough
  
   case "hansenyu\n":
  
         fmt.Printf("Welcome %s\n", input)
  
   default:
  
         fmt.Printf("You are not welcome here! Goodbye!\n")
  
   }
  

  
   // version 3:
  
   switch input {
  
   case "yinzhengjie\n", "wuzhiguang\n":
  
         fmt.Printf("Welcome %s", input)
  
   default:
  
         fmt.Printf("You are not welcome here! Goodbye!\n")
  
   }
  

  
*/
  

  
}
  

  

  

  
#以上代码执行结果如下:
  
Please enter your name:yinzhengjie
  
Your name is yinzhengjie
  
Welcome yinzhengjie!
  

  四.小试牛刀
  以下是一个和用户进行交互的程序,将用户输入的字符串和数字进行打印,相当于一个echo的一个功能,具体需要打印的类型需要自行修改。
  

/*  
#!/usr/bin/env gorun
  
@author :yinzhengjie
  
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
  
EMAIL:y1053419035@qq.com
  
*/
  

  

  
package main
  

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

  
var   (
  
   String string
  
   Number int
  
   Input string
  
)
  

  
func main(){
  
   f := bufio.NewReader(os.Stdin) //读取输入的内容
  
   for {
  
         fmt.Print("请输入一些字符串>")
  
         Input,_ = f.ReadString('\n') //定义一行输入的内容分隔符。
  
         if len(Input) == 1 {
  
             continue //如果用户输入的是一个空行就让用户继续输入。
  
         }
  
         fmt.Printf("您输入的是:%s",Input)
  
         fmt.Sscan(Input,&String,&Number) //将Input
  
         if String == "stop" {
  
             break
  
         }
  
         fmt.Printf("您输入的第一个参数是:·\033[31;1m%v\033[0m·,输入的第二个参数是··\033[31;1m%v\033[0m·.\n",String,Number)
  
   }
  
}
  

  

  
#以上代码执行结果如下:
  
请输入一些字符串>yinzhengjie 123
  
您输入的是:yinzhengjie 123
  
您输入的第一个参数是:·yinzhengjie·,输入的第二个参数是··123·.
  
请输入一些字符串>
  
请输入一些字符串>
  
请输入一些字符串>
  



页: [1]
查看完整版本: Golang的交互模式进阶