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
+7.1k Golang : Join lines with certain suffix symbol example
+6.4k Apt-get to install and uninstall Golang
+11.5k Golang : How to flush a channel before the end of program?
+16k Golang : Read a file line by line
+6.4k Golang & Javascript : How to save cropped image to file on server
+8.7k Golang : How to check variable or object type during runtime?
+5.4k Golang : Generate Interleaved 2 inch by 5 inch barcode
+6.5k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+9.7k Golang : Read file with ioutil
+15.1k Golang : How to check for empty array string or string?
+11k Golang : Command line file upload program to server example
+51.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate