Golang fmt.Sscanln function
last modified May 8, 2025
This tutorial explains how to use the fmt.Sscanln
function in Go.
We'll cover string scanning basics with practical examples of parsing formatted
strings.
The fmt.Sscanln function scans space-separated values from a string. It stops scanning at a newline and requires the input to end with a newline or EOF. It returns the number of successfully scanned items.
In Go, fmt.Sscanln
is similar to fmt.Scanln
but reads
from a string instead of standard input. It's useful for parsing structured
string data into variables.
Basic string scanning example
The simplest use of fmt.Sscanln
parses basic values from a string.
This example demonstrates scanning integers and strings.
Note: The input string must match the expected format exactly.
package main import ( "fmt" ) func main() { var name string var age int input := "John 25\n" n, err := fmt.Sscanln(input, &name, &age) if err != nil { fmt.Println("Error scanning:", err) return } fmt.Printf("Scanned %d items: %s is %d years old\n", n, name, age) }
The function scans two values from the input string. It returns the count of successfully scanned items and any error that occurred during scanning.
Scanning multiple values
fmt.Sscanln
can parse multiple values of different types at once.
This example shows scanning mixed data types from a string.
package main import ( "fmt" ) func main() { var product string var price float64 var inStock bool input := "Laptop 1299.99 true\n" n, err := fmt.Sscanln(input, &product, &price, &inStock) if err != nil { fmt.Println("Error scanning:", err) return } fmt.Printf("Scanned %d items: %s costs $%.2f (in stock: %t)\n", n, product, price, inStock) }
The example scans a string, float, and boolean from the input. The function automatically converts values to the target variable types when possible.
Handling scanning errors
Error handling is important when scanning unpredictable input. This example
demonstrates proper error handling with fmt.Sscanln
.
package main import ( "fmt" ) func main() { var x, y int tests := []string{ "10 20\n", "10 abc\n", "10\n", } for _, input := range tests { n, err := fmt.Sscanln(input, &x, &y) if err != nil { fmt.Printf("Input: %q Error: %v (scanned %d items)\n", input, err, n) continue } fmt.Printf("Scanned successfully: x=%d y=%d\n", x, y) } }
The code tests different input scenarios and handles errors appropriately. Notice how it reports partial scanning results when errors occur.
Scanning with pointers
fmt.Sscanln
requires pointers to variables to store scanned values.
This example shows proper pointer usage and common mistakes to avoid.
package main import ( "fmt" ) func main() { var a int var b float64 var c string // Correct way: passing pointers input1 := "42 3.14 hello\n" n1, err1 := fmt.Sscanln(input1, &a, &b, &c) if err1 != nil { fmt.Println("Error:", err1) } else { fmt.Printf("Scanned %d values: %d, %.2f, %q\n", n1, a, b, c) } // Incorrect way: passing values (will not compile) // n2, err2 := fmt.Sscanln(input1, a, b, c) }
The example contrasts correct pointer usage with incorrect value passing. Remember that all target variables must be passed as pointers.
Scanning into struct fields
We can scan directly into struct fields using pointers. This example demonstrates
structured data parsing with fmt.Sscanln
.
package main import ( "fmt" ) type Person struct { Name string Age int City string } func main() { var p Person input := "Alice 30 NewYork\n" n, err := fmt.Sscanln(input, &p.Name, &p.Age, &p.City) if err != nil { fmt.Println("Error scanning:", err) return } fmt.Printf("Scanned %d fields: %+v\n", n, p) }
The code scans values directly into a struct's fields. This pattern is useful for converting string data into structured Go types.
Custom type scanning
fmt.Sscanln
works with custom types that implement the Scanner
interface. This example shows how to enable scanning for custom types.
package main import ( "fmt" "strings" ) type RGB struct { R, G, B int } func (c *RGB) Scan(f fmt.ScanState, verb rune) error { _, err := fmt.Fscanf(f, "%d,%d,%d", &c.R, &c.G, &c.B) return err } func main() { var color RGB input := "255,128,0\n" _, err := fmt.Sscanln(input, &color) if err != nil { fmt.Println("Error scanning color:", err) return } fmt.Printf("Scanned color: R=%d G=%d B=%d\n", color.R, color.G, color.B) }
The RGB
type implements the Scanner
interface.
This allows fmt.Sscanln
to parse custom formatted data.
Scanning with varying input lengths
We can handle input with varying numbers of values using scanning. This example
shows flexible input parsing with fmt.Sscanln
.
package main import ( "fmt" ) func main() { inputs := []string{ "100\n", "100 200\n", "100 200 300\n", } for _, input := range inputs { var a, b, c int n, err := fmt.Sscanln(input, &a, &b, &c) if err != nil && n < 3 { fmt.Printf("Input: %q Scanned %d values: a=%d b=%d c=%d\n", input, n, a, b, c) continue } fmt.Printf("Fully scanned: a=%d b=%d c=%d\n", a, b, c) } }
The code handles inputs with different numbers of values. It reports partial scanning results when the input doesn't contain all expected values.
Source
This tutorial covered the fmt.Sscanln
function in Go with practical
examples of string parsing and scanning techniques.
Author
List all Golang tutorials.