Browse Source

Add space removal

master
joop 2 months ago
parent
commit
2fee340661
  1. 13
      prompt.go

13
prompt.go

@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"strings"
)
type Prompter interface {
@ -22,8 +23,16 @@ func Read[T any]() (out T, err error) { @@ -22,8 +23,16 @@ func Read[T any]() (out T, err error) {
*o = s[:len(s)-1]
case *Prompter:
(*o).Prompt(s)
// If a number, remove all spaces before scanning
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float32, *float64, *complex64, *complex128:
s = strings.Replace(s, " ", "", -1)
_, err = fmt.Sscan(s, o)
if err != nil {
return nilv, err
}
default:
_, err = fmt.Sscan(s, &out)
// Scan any other type, return error if it fails
_, err = fmt.Sscan(s, o)
if err != nil {
return nilv, err
}
@ -34,7 +43,7 @@ func Read[T any]() (out T, err error) { @@ -34,7 +43,7 @@ func Read[T any]() (out T, err error) {
func MustRead[T any]() T {
out, err := Read[T]()
if err != nil {
panic("Error during read: "+err.Error())
panic("Error during read: " + err.Error())
}
return out
}

Loading…
Cancel
Save