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
+50.4k Golang : How to get struct field and value by name
+9.2k Golang : Convert file unix timestamp to UTC time example
+6.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+16.2k Golang : Clone with pointer and modify value
+10.6k Golang : convert(cast) float to string
+4.5k Golang : Shortening import identifier
+22.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+11.2k Golang : Extract part of string with regular expression
+19.9k Golang : How to get time zone and load different time zone?
+15.6k Golang : Get input from keyboard
+4.7k Golang : Markov chains to predict probability of next state example
+6.8k Android Studio : Rating bar example