golang concat string and int
To concatenate a string and an int in Go (Golang), you can use the strconv
package to convert the int to a string, and then use the +
operator to concatenate the two strings together.
Here are the steps to concatenate a string and an int in Go:
- Import the
strconv
package at the top of your Go file:
import "strconv"
- Declare a variable of type int:
var num int = 42
- Convert the int to a string using the
strconv.Itoa()
function:
strNum := strconv.Itoa(num)
- Declare a string variable and assign it a value:
var str string = "The number is: "
- Concatenate the string and the converted int using the
+
operator:
result := str + strNum
Now, the variable result
will contain the concatenated string and int. In this example, it will be "The number is: 42".