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.1k Golang : Detect face in uploaded photo like GPlus
+11k Golang : Forwarding a local port to a remote server example
+20.5k Golang : How to run Golang application such as web server in the background or as daemon?
+10.9k Golang : 2 dimensional array example
+16.3k Golang : Read data from config file and assign to variables
+15.1k Golang : Convert slice to array
+8.2k Golang : Changing a RGBA image number of channels with OpenCV
+13.5k Golang : Get HTTP protocol version example
+6.7k Javascript : Put image into Chrome browser's console
+7.7k Golang : Find duplicate files with filepath.Walk
+21.6k Golang : Randomly pick an item from a slice/array example