fmt

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are the same as in C (not the ones used in Go):

%%	literal percent sign

%d	integer, base 10, signed
%u	integer, base 10, unsigned
%o	integer, base 8, unsigned
%x	integer, base 16, unsigned

%f	floating-point, decimal notation
%e	floating-point, decimal exponent notation
%a	floating-point, hexadecimal exponent notation
%g	floating-point, decimal or exponent notation as needed

%c	single literal character
%s	character string

%p	pointer, base 16 notation, with leading 0x

Index

Examples

Constants

View Source
const BufSize = 1024

BufSize is the size of the internal formatting buffer in bytes.

Variables

View Source
var (
	ErrPrint = errors.New("print failure")
	ErrScan  = errors.New("scan failure")
	ErrSize  = errors.New("buffer size exceeded")
)

Functions

func Fprintf

func Fprintf(w io.Writer, format string, a ...any) (int, error)

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered. Returns ErrSize if the output size exceeds BufSize.

Example
package main

import (
	"solod.dev/so/fmt"
	"solod.dev/so/strings"
)

func main() {
	const name, age = "Kim", 22
	var sb strings.Builder
	n, err := fmt.Fprintf(&sb, "%s is %d years old.\n", name, age)

	// The n and err return values from Fprintf are
	// those returned by the underlying io.Writer.
	if err != nil {
		panic(err)
	}
	fmt.Print(sb.String())
	fmt.Printf("%d bytes written.\n", n)
	sb.Free()

}
Output:
Kim is 22 years old.
21 bytes written.

func Fscanf

func Fscanf(r io.Reader, format string, a ...any) (int, error)

Fscanf scans text read from r, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.

func Print

func Print(a ...string) (int, error)

Print writes its arguments to standard output, separated by spaces. It returns the number of bytes written and any write error encountered.

Since Print only accepts string arguments, most of the time you'd want to use the print built-in function instead.

Example
package main

import (
	"solod.dev/so/fmt"
)

func main() {
	const name, age = "Kim", "22"
	fmt.Print(name, " is ", age, " years old.\n")

	// It is conventional not to worry about any
	// error returned by Print.

}
Output:
Kim is 22 years old.

func Printf

func Printf(format string, a ...any) (int, error)

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

Example
package main

import (
	"solod.dev/so/fmt"
)

func main() {
	const name, age = "Kim", 22
	fmt.Printf("%s is %d years old.\n", name, age)

	// It is conventional not to worry about any
	// error returned by Printf.

}
Output:
Kim is 22 years old.

func Println

func Println(a ...string) (int, error)

Println is like Print but adds a newline at the end.

Since Println only accepts string arguments, most of the time you'd want to use the println built-in function instead.

Example
package main

import (
	"solod.dev/so/fmt"
)

func main() {
	const name, age = "Kim", "22"
	fmt.Println(name, "is", age, "years old.")

	// It is conventional not to worry about any
	// error returned by Println.

}
Output:
Kim is 22 years old.

func Scanf

func Scanf(format string, a ...any) (int, error)

Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.

func Sprintf

func Sprintf(buf Buffer, format string, a ...any) string

Sprintf formats according to a format specifier, outputs to buf, and returns the resulting string. If the output size exceeds buf length, it silently truncates the output.

Example
package main

import (
	"solod.dev/so/fmt"
)

func main() {
	const name, age = "Kim", 22
	buf := fmt.NewBuffer(64)
	s := fmt.Sprintf(buf, "%s is %d years old.\n", name, age)
	fmt.Print(s)

}
Output:
Kim is 22 years old.

func Sscanf

func Sscanf(str string, format string, a ...any) (int, error)

Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.

Example
package main

import (
	"solod.dev/so/fmt"
)

func main() {
	var name string
	var age int
	n, err := fmt.Sscanf("Kim is 22 years old", "%s is %d years old", &name, &age)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%d: %s, %d\n", n, name, age)

}
Output:
2: Kim, 22

Types

type Buffer

type Buffer struct {
	Ptr *c.Char
	Len int
}

Buffer is a fixed-size stack-allocated buffer for formatted output and scanning.

func BufferFrom

func BufferFrom(buf []byte) Buffer

BufferFrom creates a Buffer that uses the provided byte slice as its storage. The buffer doesn't take ownership of the slice and doesn't free it.

func NewBuffer

func NewBuffer(size int) Buffer

NewBuffer creates a new stack-allocated Buffer of the given size.

func (Buffer) String

func (b Buffer) String() string

String returns the contents of the Buffer as a string, up to the first null byte.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL