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
+15.4k JavaScript/JQuery : Detect or intercept enter key pressed example
+12.1k Golang : Determine if time variables have same calendar day
+12.5k Elastic Search : Return all records (higher than default 10)
+12.4k Golang : Print UTF-8 fonts on image example
+14k Golang : unknown escape sequence error
+18.4k Golang : Get path name to current directory or folder
+8.9k Golang : Get final balance from bit coin address example
+16k Golang : Get digits from integer before and after given position example
+19.4k Golang : Check whether a network interface is up on your machine
+4.4k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+11.8k Android Studio : Create custom icons for your application example
+6.2k Golang : Scan forex opportunities by Bollinger bands