Join String and Integer
To join a string and integer in Go language, convert integer value to string. We may use strconv.ItoA() function to convert integer to string.
In this tutorial, we will go through examples that join a string and int values to form a resulting string.
Reference – Convert Int to String in Go
Examples
In the following example, we concatenate or join a string str
and an integer n
, and store the resulting string in result
.
Example.go
</>
Copy
package main
import "strconv"
func main() {
var str string = "Hello"
var n int = 25526
result := str + strconv.Itoa(n)
println(result)
}
Output
Hello25526
We can join two or more strings and integers in a single statement.
In the following example, we join a string, integer, then two strings, and then an integer, in a single line.
Example.go
</>
Copy
package main
import "strconv"
func main() {
result := "Hello" + strconv.Itoa(852) + "World" + "XYZ" + strconv.Itoa(99)
println(result)
}
Output
Hello852WorldXYZ99
Conclusion
In this Golang Tutorial, we learned how to concatenate or join a String and an Integer in Go language, with example programs.