Handling stdin in Go: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
=Handling <tt>stdin</tt> with <tt>fmt</tt> Functions=
=Handling <tt>stdin</tt> with <tt>fmt</tt> Functions=
==<tt>fmt.Scan()</tt>==
==<tt>fmt.Scan()</tt>==
{{External|https://golang.org/pkg/fmt/#Scan}}
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
var s string
var s string
Line 10: Line 11:
fmt.Printf("input line: %s, cnt: %d, error: %s\n", s, cnt, err)
fmt.Printf("input line: %s, cnt: %d, error: %s\n", s, cnt, err)
</syntaxhighlight>
</syntaxhighlight>
==<tt>fmt.Scanf()</tt>==
==<tt>fmt.Scanf()</tt>==
{{External|https://golang.org/pkg/fmt/#Scanf}}
{{External|https://golang.org/pkg/fmt/#Scanf}}

Revision as of 01:56, 23 August 2023

Internal

Handling stdin with fmt Functions

fmt.Scan()

https://golang.org/pkg/fmt/#Scan
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)

fmt.Scanln()

https://golang.org/pkg/fmt/#Scanln
var line string
fmt.Scanln(&line)