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.9k Golang : read gzipped http response
+14.6k Android Studio : Use image as AlertDialog title with custom layout example
+13.8k Golang : Activate web camera and broadcast out base64 encoded images
+17.8k Golang : delete and modify XML file content
+5.5k Golang : fmt.Println prints out empty data from struct
+20.9k Golang : Underscore or snake_case to camel case example
+19.3k Golang : Check if directory exist and create if does not exist
+8.4k Golang : Configure Apache and NGINX to access your Go service example
+27.3k Golang : Find files by name - cross platform example
+9.1k Golang : How to use Gorilla webtoolkit context package properly
+16.4k Golang : How to extract links from web page ?
+32.3k Golang : Convert []string to []byte examples