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.7k Golang : Warp text string by number of characters or runes example
+9.5k Swift : Convert (cast) String to Integer
+4.2k Python : Create Whois client or function example
+16.6k Golang : Get all upper case or lower case characters from string example
+12.5k Golang : Tutorial on loading GOB and PEM files
+11.6k Golang : Transform comma separated string to slice example
+9k Golang : Get escape characters \u form from unicode characters
+7k Swift : Convert (cast) String to Float
+5.4k Grep : How to grep for strings inside binary data
+4.4k Javascript : How to loop over and parse JSON data?
+15.9k Golang : Check if IP address is version 4 or 6
+17.8k Golang : How to make function callback or pass value from function as parameter?