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.1k Golang : Multi threading or run two processes or more example
+2.6k Java : Get FX sentiment from website example
+5.8k Fix sudo yum hang problem with no output or error messages
+13.3k Golang : How to filter a map's elements for faster lookup
+14.5k Golang : Loop each day of the current month example
+11.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+11.1k Golang : Get month name from date example
+4.7k Fix yum-complete-transaction error
+5.6k Golang : Totalize or add-up an array or slice example
+6.4k Golang : Rename part of filename
+23.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+9.5k Golang : How to transmit update file to client by HTTP request example