Example of Sscan vs Sscanf vs Sscanln from FMT Package


Sscan scans the argument string, storing successive space-separated values into successive arguments. Newlines count as space.
Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format.
Sscanln is similar to Sscan, but stops scanning at a newline and after the final item there must be a newline or EOF.

Example

package main

import (
	"fmt"		
)

func main(){	
	var X int
	var Y int

	fmt.Printf("\nIntital X: %d, Y: %d", X, Y)

	fmt.Sscan("100\n200", &X, &Y)
	fmt.Printf("\nSscan X: %d, Y: %d", X, Y)

	fmt.Sscanf("(10, 20)", "(%d, %d)", &X, &Y)
	fmt.Printf("\nSscanf X: %d, Y: %d", X, Y)
	
	fmt.Sscanln("50\n50", &X, &Y)
	fmt.Printf("\nSscanln X: %d, Y: %d", X, Y)
}

Output

Intital X: 0, Y: 0
Sscan X: 100, Y: 200
Sscanf X: 10, Y: 20
Sscanln X: 50, Y: 20
Most Helpful This Week