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
+16.4k Golang : Aligning strings to right, left and center with fill example
+9.9k Golang : Intercept and process UNIX signals example
+4.5k Javascript : Shuffle or randomize array example
+7.6k Golang : Combine slices but preserve order example
+34.2k Golang : Validate IP address
+7.7k Golang : Find network service name from given port and protocol
+12.7k How to automatically restart your crashed Golang server
+21.2k Golang : Convert Unix timestamp to UTC timestamp
+9.1k Golang : Get login name from environment and prompt for password
+6.7k Golang : Grayscale Image
+7.5k Golang : Another camera capture GUI application with GTK and OpenCV
+7.3k Golang : Convert word to its plural form example