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
+4.7k JavaScript : Rounding number to decimal formats to display currency
+6.4k Golang : How to get capacity of a slice or array?
+5.3k Responsive Google Adsense
+13.3k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+12.5k Golang : How to display image file or expose CSS, JS files from localhost?
+9k Golang : On lambda, anonymous, inline functions and function literals
+8.2k Golang : Multiplexer with net/http and map
+46.4k Golang : Read tab delimited file with encoding/csv package
+19.5k Golang : Display list of time zones with GMT
+7.6k Golang : How to detect if a sentence ends with a punctuation?
+17.9k How to enable MariaDB/MySQL logs ?