Handling stdin in Go: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:
cnt, err := fmt.Scanf("%f", &f)
cnt, err := fmt.Scanf("%f", &f)
</syntaxhighlight>
</syntaxhighlight>
==<tt>fmt.Scanln()</tt>==
{{External|https://golang.org/pkg/fmt/#Scanln}}
<pre>
var line string
fmt.Scanln(&line)
</pre>

Revision as of 01:58, 23 August 2023

Internal

Handling stdin with fmt Functions

fmt.Scan()

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

Read text from stdin, 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.

var s string
cnt, err := fmt.Scan(&s)
fmt.Printf("input line: %s, cnt: %d, error: %s\n", s, cnt, err)

fmt.Scanf()

https://golang.org/pkg/fmt/#Scanf
var f float
cnt, err := fmt.Scanf("%f", &f)