Library contents

Go fmt.Sprintf

Insert a student or parcel identifier into a reusable display template.

A simple example

fmt.Sprintf

  1. Start with Text A, template [value]
  2. Do this Insert text between brackets
  3. Result "[A]"
Formatting keeps the brackets and fills the single space reserved for a value.

Syntax

Go syntax
fmt.Sprintf("ID [%s]", value)
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Print a student badge

A printer receives badge or ticket data in separate fields. Use the display format required by the desk so names, numbers and separators occupy their intended places on the finished label.

From the programvalue := "Ada"
ResultID [Ada]
main.go
package main

import (
    "fmt"
)

func main() {
    value := "Ada"
    fmt.Println(fmt.Sprintf("ID [%s]", value))
}
Output
ID [Ada]

The brackets come from the template.

Advertisements

Print a parcel badge

A parcel badge combines data supplied by the shipping system into one printable line. Format those fields in the order the warehouse worker needs to read them during a handoff.

From the programvalue := "PK-17"
ResultID [PK-17]
main.go
package main

import (
    "fmt"
)

func main() {
    value := "PK-17"
    fmt.Println(fmt.Sprintf("ID [%s]", value))
}
Output
ID [PK-17]

The identifier is inserted unchanged.

Print the first entry tag

A coat-check desk numbers the first ticket one. The ticket printer includes the number as ordinary decimal text after a prefix. What does the completed label say?

GivenPrefix “Ticket ” + 1
Worked resultTicket 1

What to watch for

Treat the template as application code and pass user-provided values as arguments.

Documentation for fmt.Sprintf