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 ¶
- Constants
- Variables
- func Fprintf(w io.Writer, format string, a ...any) (int, error)
- func Fscanf(r io.Reader, format string, a ...any) (int, error)
- func Print(a ...string) (int, error)
- func Printf(format string, a ...any) (int, error)
- func Println(a ...string) (int, error)
- func Scanf(format string, a ...any) (int, error)
- func Sprintf(buf Buffer, format string, a ...any) string
- func Sscanf(str string, format string, a ...any) (int, error)
- type Buffer
Examples ¶
Constants ¶
const BufSize = 1024
BufSize is the size of the internal formatting buffer in bytes.
Variables ¶
Functions ¶
func Fprintf ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Buffer is a fixed-size stack-allocated buffer for formatted output and scanning.
func BufferFrom ¶
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.