gcmd_scan.go 858 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. package gcmd
  8. import (
  9. "bufio"
  10. "fmt"
  11. "os"
  12. "github.com/gogf/gf/text/gstr"
  13. )
  14. // Scan prints <info> to stdout, reads and returns user input, which stops by '\n'.
  15. func Scan(info ...interface{}) string {
  16. fmt.Print(info...)
  17. return readline()
  18. }
  19. // Scanf prints <info> to stdout with <format>, reads and returns user input, which stops by '\n'.
  20. func Scanf(format string, info ...interface{}) string {
  21. fmt.Printf(format, info...)
  22. return readline()
  23. }
  24. func readline() string {
  25. var s string
  26. reader := bufio.NewReader(os.Stdin)
  27. s, _ = reader.ReadString('\n')
  28. s = gstr.Trim(s)
  29. return s
  30. }