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
+6.6k Grep : How to grep for strings inside binary data
+7.8k Golang : Scan files for certain pattern and rename part of the files
+8.7k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+24.6k Golang : Time slice or date sort and reverse sort example
+10.6k Golang : Flip coin example
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+5k Python : Find out the variable type and determine the type with simple test
+9.5k Golang : Qt Yes No and Quit message box example
+6.1k Golang : Create new color from command line parameters
+8.7k Golang : Progress bar with ∎ character
+6.1k PageSpeed : Clear or flush cache on web server
+6.9k Get Facebook friends working in same company