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
+8.6k Golang : Get final balance from bit coin address example
+19.3k Golang : Example for DSA(Digital Signature Algorithm) package functions
+8.2k Golang : Generate Datamatrix barcode
+7.3k Golang : Shuffle strings array
+20.5k PHP : Convert(cast) int to double/float
+14.2k Golang : How to check if your program is running in a terminal
+6.6k Default cipher that OpenSSL used to encrypt a PEM file
+10.3k Android Studio : Simple input textbox and intercept key example
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+6.2k Golang : Break string into a slice of characters example
+30.2k Golang : Generate random string
+9k Golang : Generate random Chinese, Japanese, Korean and other runes