I'm very new to Go, and am trying to get some experience by re-writing some of my Python code in Go. Below is a function that takes an IP address (ipv4) in integer form and returns the string version. From 572779703
to 34.35.236.183
for example.
func int2ip(num int) (string) {
result := [4]int{}
for i, n := range []uint{24, 16, 8, 0} {
result[i] = (num >> n) & 255
}
return fmt.Sprintf("%d.%d.%d.%d", result[0], result[1], result[2], result[3])
}
I'm not only curious if there is a more straightforward or concise way to do this with Go, but am very curious if there is a way to use the []result
array in Sprintf()
without having to type out every index.
Something similar to:
fmt.Sprintf("%d.%d.%d.%d", result)