Skip to content

fmt Module

The fmt module provides functions for formatting and printing output to the console.

Functions

Println(args...)

Prints the given arguments to the console, followed by a newline character.

Parameters: - args...: A variable number of arguments of any type.

Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.

Example:

Println Example
1
2
3
4
5
6
package main
import fmt

fmt.Println("Hello, World!")
var x = 42
fmt.Println("The value of x is:", x)


Print(args...)

Prints the given arguments to the console without a newline character at the end.

Parameters: - args...: A variable number of arguments of any type.

Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.

Example:

Print Example
1
2
3
4
5
6
package main
import fmt

fmt.Print("Hello, ")
fmt.Print("World!")
// Output: Hello, World!


Printf(format, args...)

Prints formatted output to the console. It uses a format string and a variable number of arguments.

Parameters: - format: A string that contains format specifiers. - args...: The arguments to be formatted.

Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.

Format Specifiers: - %v: The default format for the value. - %s: A string. - %d: A decimal integer. - %f: A floating-point number.

Example:

Printf Example
1
2
3
4
5
6
package main
import fmt

var name = "Alice"
var age = 30
fmt.Printf("Name: %s, Age: %d", name, age)


Errorf(format, args...)

Creates a new error with a formatted error message.

Parameters: - format: A string that contains format specifiers. - args...: The arguments to be formatted.

Returns: - (error, None): A new error object.

Example:

Errorf Example
package main
import fmt

function divide(a int, b int) (int, error) {
    if b == 0 {
        // fmt.Errorf returns (error, error); destructure to get the error value
        var e, _ = fmt.Errorf("division by zero: %d / %d", a, b)
        return 0, e
    }
    return a / b, None
}

var result, err = divide(10, 0)
if err != None {
    fmt.Println(err)
}


Sprint(args...)

Builds and returns a string by concatenating the default format of each argument separated by spaces. No trailing newline.

Parameters: - args...: Arguments to concatenate.

Returns: - string

Example:

Sprint Example
1
2
3
4
5
package main
import fmt

var s = fmt.Sprint("alpha", 123, true)
fmt.Println("Sprint:", s)


Sprintln(args...)

Like Sprint but appends a trailing newline to the result.

Parameters: - args...: Arguments to concatenate.

Returns: - string

Example:

Sprintln Example
1
2
3
4
5
6
package main
import fmt

var line = fmt.Sprintln("line", 456)
// Print to show the raw value (includes newline)
fmt.Print(line)


Sprintf(format, args...)

Formats according to a format specifier and returns the resulting string.

Parameters: - format: Format string (supports %s, %d, %f, %t, %v, %+v, %#v, %T). - args...: Arguments to format.

Returns: - string

If the first argument is not a string or there are fewer arguments than required by the format string, a runtime error is raised.

Example:

Sprintf Format
1
2
3
4
5
package main
import fmt

var s = fmt.Sprintf("name=%s id=%d ok=%t", "bob", 7, true)
fmt.Println("Sprintf:", s)


Stdout() and Stderr()

Return handles that represent standard output and standard error. These handles can be used with Fprint, Fprintln, and Fprintf.

Returns: - (any, error)

Example:

Stdout and Stderr
package main
import fmt

var out, outErr = fmt.Stdout()
if outErr == None {
    var _, e1 = fmt.Fprintln(out, "hello stdout")
}

var errOut, errOutErr = fmt.Stderr()
if errOutErr == None {
    var _, e2 = fmt.Fprintln(errOut, "hello stderr")
}


Fprint(w, args...)

Writes arguments to the given writer handle without a trailing newline.

Parameters: - w: Writer handle (use fmt.Stdout() or fmt.Stderr()). - args...: Values to write.

Returns: - (None, error)

Example:

Fprint to Writer
1
2
3
4
5
package main
import fmt

var out, _ = fmt.Stdout()
var _, err = fmt.Fprint(out, "Fprint ", 123, " ok")


Fprintln(w, args...)

Writes arguments to the writer followed by a newline.

Parameters: - w: Writer handle. - args...: Values to write.

Returns: - (None, error)

Example:

Fprintln to Writer
1
2
3
4
5
package main
import fmt

var out, _ = fmt.Stdout()
var _, err = fmt.Fprintln(out, "Fprintln line")


Fprintf(w, format, args...)

Formats according to a format specifier and writes to the writer.

Parameters: - w: Writer handle. - format: Format string. - args...: Arguments to format.

Returns: - (None, error)

Example:

Fprintf to Writer
1
2
3
4
5
package main
import fmt

var out, _ = fmt.Stdout()
var _, err = fmt.Fprintf(out, "id=%d ok=%t\n", 42, true)


Scan()

Reads a line from standard input and splits it by whitespace into fields.

Returns: - (array, error)


Scanln()

Reads a line from standard input and returns it as a string (without trailing newline).

Returns: - (string, error)


Scanf(format)

Reads a line from standard input and parses it according to the format string. Supported verbs: %s, %d, %f, %t.

Parameters: - format: Format string.

Returns: - (array, error)

Note: The Scan* functions block waiting for input. Use them interactively or in dedicated examples.