Golang cast to string: int, any, and bool with strconv and fmt

Golang cast to string and go cast to string: golang int to string and convert int to string golang with strconv and fmt; golang convert any to string and golang cast any to string with type switches and fmt.Sprint; bool with FormatBool—Go has no C-style string cast for numbers.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang cast to string: int, any, and bool with strconv and fmt

People search for golang cast, golang cast to string, go cast to string, or golang convert any to string when they want a string from another type. Go does not offer a single safe “cast” from int to decimal text: string(rune) is for runes, not numbers. For golang int to string, go int to string, convert int to string golang, and int to string golang, use strconv or fmt. For golang any to string or golang cast any to string, combine any (alias of interface{}) with a type switch or fmt.Sprint depending on whether you need a real conversion or a debug representation. Deeper coverage: strconv in Go, int to string, interface to string, bool to string.

Tested with Go 1.24 on Linux.


Golang int to string

Use strconv.Itoa for int, strconv.FormatInt with base 10 for int64, or fmt.Sprintf("%d", ...) when formatting into a larger template.

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n := 42
	fmt.Println(strconv.Itoa(n))
	fmt.Println(strconv.FormatInt(int64(n), 10))
	fmt.Println(fmt.Sprintf("%d", n))
}
Output

You should see three lines, each 42. For more options and bases, see golang-convert-int-to-string.


Golang convert any to string (interface{} / any)

fmt.Sprint(v) or fmt.Sprintf("%v", v) turns any value into some string, which is enough for logs but not a substitute for parsing rules on the wire. For a true golang cast any to string style workflow, branch on the dynamic type:

go
package main

import (
	"fmt"
	"strconv"
)

func anyToString(v any) string {
	switch t := v.(type) {
	case string:
		return t
	case int:
		return strconv.Itoa(t)
	case int64:
		return strconv.FormatInt(t, 10)
	case fmt.Stringer:
		return t.String()
	default:
		return fmt.Sprint(v)
	}
}

func main() {
	fmt.Println(anyToString("hello"))
	fmt.Println(anyToString(7))
	fmt.Println(anyToString([]int{1, 2}))
}
Output

You should see hello, then 7, then a slice print form like [1 2]. For any that might hold []byte or nested interfaces, extend the switch or follow interface to string.


Bool to string

strconv.FormatBool returns "true" or "false". fmt.Sprintf("%t", b) does the same for formatting.

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatBool(true))
	fmt.Println(fmt.Sprintf("%t", false))
}
Output

You should see true then false. More patterns: golang-bool-to-string.


Summary

Golang cast to string is really conversion or formatting: numbers use strconv or %d, not string(int). Golang int to string and go int to string are covered by Itoa, FormatInt, and Sprintf. Golang any to string and golang convert any to string need a type switch (or assertions) when the result must be correct for each type; fmt.Sprint is fine when you only need a readable representation. Use the linked articles for interfaces, flags, and edge cases.


References


Frequently Asked Questions

1. Can I use a cast like string(myInt) in Go?

No; string(int) treats the int as a Unicode code point rune, not a decimal digit string, so strconv.Itoa, strconv.FormatInt, or fmt.Sprintf with %d are correct for golang int to string.

2. How do I golang int to string or go int to string?

Use strconv.Itoa for int, strconv.FormatInt(i, 10) for int64 and other sizes, or fmt.Sprintf("%d", i); see strconv docs and a focused int guide in the links below.

3. How do I golang convert any to string or golang cast any to string?

any is an alias for interface{}; use a type switch or type assertion for real conversions, or fmt.Sprint or fmt.Sprintf("%v") when you only need a readable representation, not a stable wire format.

4. When is fmt.Sprint enough for golang any to string?

For logs and debugging; for APIs and storage prefer explicit strconv or a type switch so the format does not change when types change.

5. Where can I read more on interface and bool to string?

Use the dedicated guides for interface values and booleans linked in References; this page is an overview of common patterns.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …