In Go, you can convert an integer to its binary representation as a string using the strconv
package. Here's an example that demonstrates how to convert an integer to binary:
package main
import (
"fmt"
"strconv"
)
func main() {
// Convert integer to binary
num := 42
binaryString := strconv.FormatInt(int64(num), 2)
fmt.Println(binaryString) // Output: 101010
}
In this example, we use the strconv.FormatInt()
function to convert the integer to a binary string. The second argument 2
specifies the base, which in this case is binary.