Sure, in Go, you can convert any data type to a string using the fmt
package. Here’s an example:
package main
import (
"fmt"
)
func main() {
// Convert an int to a string
num := 123
str := fmt.Sprintf("%d", num)
fmt.Println(str) // Output: "123"
// Convert a float to a string
floatNum := 3.14
str = fmt.Sprintf("%f", floatNum)
fmt.Println(str) // Output: "3.140000"
// Convert a boolean to a string
boolVal := true
str = fmt.Sprintf("%t", boolVal)
fmt.Println(str) // Output: "true"
}
In this example, we use fmt.Sprintf()
function to convert different types of values into strings. We pass the format specifier %d
, %f
, or %t
depending on the type we want to convert, followed by the value itself.
I hope that helps! Let me know if you have any other questions.