%d is the format specifier for base 10 integers (what you typically want) a full listing of fmt's format specifiers can be found here; https://golang.org/pkg/fmt/

var count int = 5
fmt.Printf("count:%d\n", count)
// prints count:5
Answer from evanmcdonnal on Stack Overflow
🌐
Go Packages
pkg.go.dev › strconv
strconv package - strconv - Go Packages
FormatInt returns the string representation of i in the given base, for 2 <= base <= 36.
🌐
Educative
educative.io › answers › how-to-use-the-strconvformatint-function-in-go
How to use the strconv.FormatInt() function in Go
The function FormatInt() returns the string representation of the given integer in the given base.
Discussions

go - What is the correct string format specifier for integer in fmt.Printf? - Stack Overflow
var count int = 5 fmt.Printf("count:%i\n", count) Its output is count:%!i(int=5) What is the correct format specifier so that the output is count:5 I look up the package fmt's method Printf in... More on stackoverflow.com
🌐 stackoverflow.com
Format a Go string without printing? - Stack Overflow
Is there a simple way to format a string in Go without printing the string? I can do: bar := "bar" fmt.Printf("foo: %s", bar) But I want the formatted string returned rather than printed so I can More on stackoverflow.com
🌐 stackoverflow.com
proposal: strconv: Make FormatInt accept any integer type
Description Change strconv.FormatInt to be a generic that accepts any integer type. Deprecate strconv.FormatUint and suggest to use strconv.FormatInt instead. Motivation During a review of a medium-sized codebase I have noticed that ther... More on github.com
🌐 github.com
2
February 8, 2023
How to convert an int value to string in Go? - Stack Overflow
i := 123 s := string(i) s is 'E', but what I want is "123" Please tell me how can I get "123". More on stackoverflow.com
🌐 stackoverflow.com
🌐
ZetCode
zetcode.com › golang › strconv-formatint
Using strconv.FormatInt in Go
The simplest use of strconv.FormatInt converts an integer to a base 10 string.
🌐
IncludeHelp
includehelp.com › golang › strconv-formatint-function-with-examples.aspx
Golang strconv.FormatInt() Function with Examples
// Golang program to demonstrate the // example of strconv.FormatInt() Function package main import ( "fmt" "strconv" ) func main() { var x int64 var y int64 var result string x = 108 result = strconv.FormatInt(x, 10) fmt.Printf("x: Type %T, value %v\n", x, x) fmt.Printf("result: Type %T, value %q\n", result, result) fmt.Println() y = 64 result = strconv.FormatInt(y, 10) fmt.Printf("y: Type %T, value %v\n", y, y) fmt.Printf("result: Type %T, value %q\n", result, result) fmt.Println() z := x + y result = strconv.FormatInt(z, 10) fmt.Printf("z: Type %T, value %v\n", z, z) fmt.Printf("result: Type %T, value %q\n", result, result) }
🌐
GitHub
github.com › hellokoding › golang › blob › master › golang-core › formatint.go
golang/golang-core/formatint.go at master · hellokoding/golang
Golang Tutorals and Examples. Contribute to hellokoding/golang development by creating an account on GitHub.
Author   hellokoding
🌐
Go Packages
pkg.go.dev › fmt
fmt package - fmt - Go Packages
Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
Find elsewhere
🌐
Go by Example
gobyexample.com › string-formatting
Go by Example: String Formatting
Go offers excellent support for string formatting in the printf tradition. Here are some examples of common string formatting tasks · Go offers several printing “verbs” designed to format general Go values. For example, this prints an instance of our point struct
Top answer
1 of 8
736

Sprintf is what you are looking for.

Example

fmt.Sprintf("foo: %s", bar)

You can also see it in use in the Errors example as part of "A Tour of Go."

return fmt.Sprintf("at %v, %s", e.When, e.What)
2 of 8
274

1. Simple strings

For "simple" strings (typically what fits into a line) the simplest solution is using fmt.Sprintf() and friends (fmt.Sprint(), fmt.Sprintln()). These are analogous to the functions without the starter S letter, but these Sxxx() variants return the result as a string instead of printing them to the standard output.

For example:

s := fmt.Sprintf("Hi, my name is %s and I'm %d years old.", "Bob", 23)

The variable s will be initialized with the value:

Hi, my name is Bob and I'm 23 years old.

Tip: If you just want to concatenate values of different types, you may not automatically need to use Sprintf() (which requires a format string) as Sprint() does exactly this. See this example:

i := 23
s := fmt.Sprint("[age:", i, "]") // s will be "[age:23]"

For concatenating only strings, you may also use strings.Join() where you can specify a custom separator string (to be placed between the strings to join).

Try these on the Go Playground.

2. Complex strings (documents)

If the string you're trying to create is more complex (e.g. a multi-line email message), fmt.Sprintf() becomes less readable and less efficient (especially if you have to do this many times).

For this the standard library provides the packages text/template and html/template. These packages implement data-driven templates for generating textual output. html/template is for generating HTML output safe against code injection. It provides the same interface as package text/template and should be used instead of text/template whenever the output is HTML.

Using the template packages basically requires you to provide a static template in the form of a string value (which may be originating from a file in which case you only provide the file name) which may contain static text, and actions which are processed and executed when the engine processes the template and generates the output.

You may provide parameters which are included/substituted in the static template and which may control the output generation process. Typical form of such parameters are structs and map values which may be nested.

Example:

For example let's say you want to generate email messages that look like this:

Hi [name]!

Your account is ready, your user name is: [user-name]

You have the following roles assigned:
[role#1], [role#2], ... [role#n]

To generate email message bodies like this, you could use the following static template:

const emailTmpl = `Hi {{.Name}}!

Your account is ready, your user name is: {{.UserName}}

You have the following roles assigned:
{{range $i, $r := .Roles}}{{if $i}}, {{end}}{{.}}{{end}}
`

And provide data like this for executing it:

data := map[string]interface{}{
    "Name":     "Bob",
    "UserName": "bob92",
    "Roles":    []string{"dbteam", "uiteam", "tester"},
}

Normally output of templates are written to an io.Writer, so if you want the result as a string, create and write to a bytes.Buffer (which implements io.Writer). Executing the template and getting the result as string:

t := template.Must(template.New("email").Parse(emailTmpl))
buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
    panic(err)
}
s := buf.String()

This will result in the expected output:

Hi Bob!

Your account is ready, your user name is: bob92

You have the following roles assigned:
dbteam, uiteam, tester

Try it on the Go Playground.

Also note that since Go 1.10, a newer, faster, more specialized alternative is available to bytes.Buffer which is: strings.Builder. Usage is very similar:

builder := &strings.Builder{}
if err := t.Execute(builder, data); err != nil {
    panic(err)
}
s := builder.String()

Try this one on the Go Playground.

Note: you may also display the result of a template execution if you provide os.Stdout as the target (which also implements io.Writer):

t := template.Must(template.New("email").Parse(emailTmpl))
if err := t.Execute(os.Stdout, data); err != nil {
    panic(err)
}

This will write the result directly to os.Stdout. Try this on the Go Playground.

🌐
GitHub
github.com › golang › go › issues › 58407
proposal: strconv: Make FormatInt accept any integer type · Issue #58407 · golang/go
February 8, 2023 - Description Change strconv.FormatInt to be a generic that accepts any integer type. Deprecate strconv.FormatUint and suggest to use strconv.FormatInt instead. Motivation During a review of a medium-sized codebase I have noticed that ther...
Author   misha-ridge
🌐
Ecostack
ecostack.dev › posts › golang-int-to-string
Golang Integer to String · Ecostack
August 8, 2023 - The strconv.Itoa(i int) function converts an integer to a string. The function name is short for “integer to ASCII”. The function takes an integer as an argument and returns a string. Internally, it is calling the FormatInt function with base 10.
🌐
Golang.cafe
golang.cafe › blog › golang-int-to-string-conversion-example
Golang Int To String Conversion Example | Golang.cafe
April 27, 2022 - Go (Golang) provides string and integer conversion directly from a package coming from the standard library - strconv. In order to convert an integer value to string in Golang we can use the FormatInt function from the strconv package.
🌐
GeeksforGeeks
geeksforgeeks.org › go language › how-to-use-strconv-formatuint-function-in-golang
How to use strconv.FormatUint() Function in Golang? - GeeksforGeeks
May 3, 2020 - // Golang program to illustrate // strconv.FormatUint() Function package main import ( "fmt" "strconv" ) func main() { // Finding the string representation // of given value in the given base // Using FormatUint() function val1 := uint64(25) res1 := strconv.FormatUint(val1, 2) fmt.Printf("Result 1: %v", res1) fmt.Printf("\nType 1: %T", res1) val2 := uint64(20) res2 := strconv.FormatUint(val2, 16) fmt.Printf("\nResult 2: %v", res2) fmt.Printf("\nType 2: %T", res2) } Output:
🌐
YourBasic
yourbasic.org › golang › fmt-printf-reference-cheat-sheet
fmt.Printf formatting tutorial and cheat sheet · YourBasic Go
CODE EXAMPLE The functions fmt.Printf and fmt.Sprintf format numbers and strings indented with spaces or zeroes, in different bases, and with optional quotes.
🌐
Medium
medium.com › @sahaidachnyi › format-specifiers-in-golang-d7f8d4fe88b8
The Power of Format Specifiers in Golang | by Taras Sahaidachnyi | Medium
May 6, 2024 - The %u format specifier is a fundamental tool for printing unsigned integer values in Golang.
🌐
ByteSizeGo
bytesizego.com › blog › convert-int-to-string-golang
How to convert an int to string in Golang
package main import ( "fmt" "strconv" ) func main() { number := int64(789) str := strconv.FormatInt(number, 10) // Base 10 conversion fmt.Println("Formatted string:", str) }
🌐
ZetCode
zetcode.com › golang › string-format
Formatting Strings in Go with the fmt Package
April 11, 2024 - Comprehensive guide to formatting strings in Go using the fmt package. Includes examples of formatting verbs and flags.