In Go, you can convert any data type to a string using the strconv
package. Here’s an example:
package main
import (
"fmt"
"strconv"
)
func main() {
// Convert an int to a string
num := 123
str := strconv.Itoa(num)
fmt.Println(str) // Output: "123"
// Convert a float to a string
floatNum := 3.14
str = strconv.FormatFloat(floatNum, 'f', -1, 64)
fmt.Println(str) // Output: "3.14"
// Convert a boolean to a string
boolVal := true
str = strconv.FormatBool(boolVal)
fmt.Println(str) // Output: "true"
}
In this example, we use strconv.Itoa()
function to convert an integer into a string. For floating-point numbers, we use strconv.FormatFloat()
function with format specifier 'f'
. Finally, for boolean values, we use strconv.FormatBool()
function.
I hope that helps! Let me know if you have any other questions.