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.2k Linux/Unix : Commands that you need to be careful about
+29.6k error: trying to remove "yum", which is protected
+20.5k Golang : Join arrays or slices example
+7.6k Golang : Executing and evaluating nested loop in html template
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+6.1k Golang : Validate credit card example
+4.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+13.7k Golang : How to check if IP address is in range
+21.2k Golang : Set and Get HTTP request headers example
+4.8k Golang : Error handling methods
+16.5k Golang : Simple client server example