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
+18.3k Golang : Get command line arguments
+18.6k Golang : Example for RSA package functions
+18.6k Golang : How to remove certain lines from a file
+32.6k Golang : Math pow(the power of x^y) example
+34.3k Golang : Create x509 certificate, private and public keys
+9.7k Golang : Read file with ioutil
+6.3k Golang : Get missing location after unmarshal binary and gob decode time.
+12.9k Golang : Convert int(year) to time.Time type
+7.1k Nginx : Password protect a directory/folder
+10.7k Golang : Select region of interest with mouse click and crop from image