Scan
Functions for reading user input from stdin.




1CCnwAvJYEoDVGM7vsBg2Q99cF9EHtBVaY
-168363?style=flat&logo=tether&logoColor=white&logoSize=auto)
0x54f0ccc6b2987de454f69f2814fc9202bcfb74fe
Table of Contents
Features
- Simple single-line input reading
- Returns
io.EOF when input is closed (Ctrl+D)
- Type conversion with error handling
Installation
go get github.com/andmitr/pkg/scan
Usage
func String
func String() (string, error)
String reads a single line from stdin and returns it as a string.
Example
fmt.Print("Enter your name: ")
name, err := scan.String()
if errors.Is(err, io.EOF) {
fmt.Println("Input closed")
return
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("Hello, %s!\n", name)
func Int
func Int() (int, error)
Int reads a single line from stdin and returns it as an int.
Example
fmt.Print("Enter your age: ")
age, err := scan.Int()
if err != nil {
log.Fatal(err)
}
fmt.Printf("You are %d years old\n", age)
func Float
func Float() (float64, error)
Float reads a single line from stdin and returns it as a float64.
Example
fmt.Print("Enter price: ")
price, err := scan.Float()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Price: %.2f\n", price)
License
MIT Licensed. See LICENSE for details.