mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Merge pull request #844 from qinyuguang/scan
gcmd.Scan supports read line that contains whitespace
This commit is contained in:
@ -7,20 +7,30 @@
|
||||
|
||||
package gcmd
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
)
|
||||
|
||||
// Scan prints <info> to stdout, reads and returns user input, which stops by '\n'.
|
||||
func Scan(info ...interface{}) string {
|
||||
var s string
|
||||
fmt.Print(info...)
|
||||
fmt.Scanln(&s)
|
||||
return s
|
||||
return readline()
|
||||
}
|
||||
|
||||
// Scanf prints <info> to stdout with <format>, reads and returns user input, which stops by '\n'.
|
||||
func Scanf(format string, info ...interface{}) string {
|
||||
var s string
|
||||
fmt.Printf(format, info...)
|
||||
fmt.Scanln(&s)
|
||||
return readline()
|
||||
}
|
||||
|
||||
func readline() string {
|
||||
var s string
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
s, _ = reader.ReadString('\n')
|
||||
s = gstr.Trim(s)
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user