Golang : How to convert(cast) IP address to string?
Problem :
You need to convert(cast) the integer values you get from function such as net.LookupIP()
or type IP (http://golang.org/pkg/net/#IP) to string.
Solution :
Each IP address returned by net.LookupIP()
has the .String()
method. See example usage :
package main
import (
"fmt"
"net"
"strings"
)
func main() {
addresses, err := net.LookupIP("www.yahoo.com")
fmt.Println(addresses, err)
for i := 0; i < len(addresses); i++ {
segments := strings.SplitAfter(addresses[i].String(), " ") //<--- here!
fmt.Printf("IP address #%d : %s \n", i, segments)
}
}
See also : Golang : How to convert(cast) string to IP address?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.5k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+5.4k PHP : Hide PHP version information from curl
+13.6k Golang : Read from buffered reader until specific number of bytes
+14.2k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+10.6k Golang : Meaning of omitempty in struct's field tag
+14.8k Golang : How to check if your program is running in a terminal
+8.4k Golang : Routes multiplexer routing example with regular expression control
+14.9k Golang : Normalize unicode strings for comparison purpose
+35k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+9.9k Golang : Eroding and dilating image with OpenCV example
+11k PHP : Convert(cast) bigInt to string
+16.2k Golang : Get sub string example