What is the Fscanln function in Golang?

The Fscanln function in the Go programming language is used to read data from a source of your choosing, format the string, and store the resultant strings into the destinations specified by a list of additional arguments you provide. It differs from the simple Fscan function because it stops the scanning process when encountering a newline character.

To use this function, you must import the fmt package in your file and access the Fscanln function within, using the . notation: fmt.Fscanln. Here Fscanln is the actual function, while fmt is the Go package that stores the definition of this function.

Function definition

The definition of the Fscanln function inside the fmt package is as follows:

Parameters

fmt.Fscanln takes a writing destination along with a list of a variable number of arguments.

  • src: The source from where the input is to be taken. This should be an object of type io.Writer.

io.Writer objects are, in simple terms, objects that have a built-in write method.

  • a ...interface{}: The list of all arguments that you want to store data in. After taking in the input, the input string is automatically split on space characters. The components are stored sequentially into the given arguments. If there are fewer arguments than the different splits of the input string, then the extra pieces are discarded.

Return values

The fmt.Fscanln function can return two things:

  • count: The number of arguments the function writes to.

  • err: Any error thrown during the execution of the function.

Example

The following example is a simple program where we read data from a buffer variable of type bytes.Buffer, store it into our desired variables, and then use the normal Print function to print a new string to the standard output.

Fscanln reads from the input source sequentially. Hence, we must give the list of arguments in the order specified in the format string.

Here, we require an input with the specific format matching the sequence of arguments we have given the Fscanln function.

For example, with the first word being a string, the second can be anything since it is stored in temp and is unused, the third can be an int, and another string after that.

Here, we are using: “Faraz owns 500 acres of land”. We then use the Fscanln function to read this input and store parts of the string corresponding to the amount of land, units of measurement, and the owner’s name. We then use Printf to print a new string.

The bytes data type has a built-in write function which makes it a compatible argument to send to the Fscanln function. To use the Buffer data type, you need to import the bytes package.

package main
import (
"fmt"
"bytes"
)
func main() {
var src_buff bytes.Buffer
//assigning our buffer a message
message := "Faraz owns 500 acres of land"
fmt.Fprint(&src_buff, message)
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using the buffer string
fmt.Fscanln(&src_buff, &name, &temp, &amount, &unit)
// print out new string using the extracted values
fmt.Printf ("%d %s of land is owned by %s\n",amount, unit, name);
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved