From ba1a9d9f8eace37533b2369aa61a4936694ccd77 Mon Sep 17 00:00:00 2001 From: qinyuguang Date: Sat, 1 Aug 2020 02:13:42 +0800 Subject: [PATCH 1/2] gcmd.Scan supports read line that contains whitespace --- os/gcmd/gcmd_scan.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/os/gcmd/gcmd_scan.go b/os/gcmd/gcmd_scan.go index 12fb98f86..c2c528a68 100644 --- a/os/gcmd/gcmd_scan.go +++ b/os/gcmd/gcmd_scan.go @@ -7,13 +7,21 @@ package gcmd -import "fmt" +import ( + "bufio" + "fmt" + "os" + + "github.com/gogf/gf/text/gstr" +) // Scan prints 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) + reader := bufio.NewReader(os.Stdin) + s, _ = reader.ReadString('\n') + s = gstr.Trim(s) return s } From cf88f285198e7f6ab19db592eeda4dcbfbb2909b Mon Sep 17 00:00:00 2001 From: qinyuguang Date: Mon, 3 Aug 2020 21:00:02 +0800 Subject: [PATCH 2/2] gcmd.Scanf supports read line that contains whitespace --- os/gcmd/gcmd_scan.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/os/gcmd/gcmd_scan.go b/os/gcmd/gcmd_scan.go index c2c528a68..492c668f0 100644 --- a/os/gcmd/gcmd_scan.go +++ b/os/gcmd/gcmd_scan.go @@ -17,18 +17,20 @@ import ( // Scan prints to stdout, reads and returns user input, which stops by '\n'. func Scan(info ...interface{}) string { - var s string fmt.Print(info...) + return readline() +} + +// Scanf prints to stdout with , reads and returns user input, which stops by '\n'. +func Scanf(format string, info ...interface{}) string { + fmt.Printf(format, info...) + return readline() +} + +func readline() string { + var s string reader := bufio.NewReader(os.Stdin) s, _ = reader.ReadString('\n') s = gstr.Trim(s) return s } - -// Scanf prints to stdout with , 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 s -}