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
+8.4k CodeIgniter : Load different view for mobile devices
+5.8k Golang : Convert(cast) io.Reader type to string
+16.2k Golang : Check if os.Stdin input data is piped or from terminal
+3.2k Which content-type(MIME type) to use for JSON data
+16.9k Golang : Read directory content with os.Open
+23.6k PHP : Count number of JSON items/objects
+17.6k Golang : Count number of digits from given integer value
+19.2k Golang : How to run Golang application such as web server in the background or as daemon?
+32.8k Upload multiple files with Go
+4.4k PageSpeed : Clear or flush cache on web server
+7.9k Golang : Bcrypting password
+36.7k Golang : How to iterate over a []string(array)