Standard library / fmt

fmt.Appendf()

Appends formatted bytes to the destination slice b and returns the resulting slice; retain the returned value because growth can allocate. Format-directed output. Expected stdout: '"id:009"\n'.

Formal Syntax & Structure

go syntaxgo
func Appendf(b []byte, format string, a ...any) []byte

Parameters & Attributes

NameTypeDescription
b[]byteDestination byte slice; existing prefix is preserved.
formatstringFormat string containing literal text and formatting or scanning directives.
a...anyValues consumed by formatting; may be empty.

Return Value

[]byte

Formatted bytes including the original prefix.

Working Example

Example Snippetgo
package main
import (  "fmt")
func main() {  b := []byte("id:")  b = fmt.Appendf(b, "%03d", 9)  fmt.Printf("%q\n", b)}
Go Reference