Standard library / fmt
fmt.Fscanf()
Reads values from an io.Reader into pointer arguments. The format controls parsing; spaces and newlines have specific matching rules. Returns number of successfully assigned items and error; partial success is possible. A successful scan need not validate all remaining input. Expected stdout: '2 12 red <nil>\n'.
Formal Syntax & Structure
go syntaxgo
func Fscanf(r io.Reader, format string, a ...any) (n int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | io.Reader | Input implementing io.Reader. |
| format | string | Format string containing literal text and formatting or scanning directives. |
| a | ...any | Destination pointers or Scanner implementations, one for each value to scan. |
Return Value
(n int, err error)
Number of assigned operands and scan error.
Working Example
Example Snippetgo
package main
import ( "fmt" "strings")
func main() { var n int var word string count, e := fmt.Fscanf(strings.NewReader("12 red"), "%d %s", &n, &word) fmt.Println(count, n, word, e)}