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
+12.6k Golang : Add ASCII art to command line application launching process
+8.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+5.8k Golang : Detect variable or constant type
+9.9k Golang : Read file and convert content to string
+7.2k Golang : Individual and total number of words counter example
+20.3k Golang : Pipe output from one os.Exec(shell command) to another command
+9.2k Golang : Generate EAN barcode
+6.6k Golang : Reverse by word
+30.2k Golang : How to redirect to new page with net/http?
+5.6k List of Golang XML tutorials
+6.8k Fix sudo yum hang problem with no output or error messages
+15.9k Golang : Get sub string example