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.
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))
}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:
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}))
}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.
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.FormatBool(true))
fmt.Println(fmt.Sprintf("%t", false))
}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
- strconv package
- fmt package
- strconv in Go
- Convert int to string in Go
- Interface to string
- Bool to string
- Type conversions

