Golang fmt.Scan() function examples

package fmt

Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

Golang fmt.Scan() function usage examples

Example 1 :

 fmt.Printf("Enter 2 numbers> ")
 fmt.Scan(&x)
 fmt.Scan(&y)
 fmt.Printf("Sending %d and %d\n", x, y)

Example 2 :

 c := make(chan string)
 go func() {
 var o string
 fmt.Scan(&o)
 c <- o
 }()

Example 3:

 ...
 var (
 readValue string
 readSize int
 )

 readSize, err = fmt.Scan(&readValue)
 if err != nil {
 return
 } else if readSize < 1 {
 err = errors.New("Can't read input")
 return
 }
 ... 

Reference :

http://golang.org/pkg/fmt/#Scan

Advertisement