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
+19k Golang : Get ASCII code from a key press(cross-platform) example
+38.6k Golang : Marshal and unmarshal json.RawMessage struct example
+5.5k Golang : Configure Apache and NGINX to access your Go service example
+6.6k Golang : Build and compile multiple source files
+9.3k Golang : Get month name from date example
+4.1k Golang : Generate multiplication table from an integer example
+5.5k Golang : Get YouTube playlist
+26.7k Golang : Generate random string
+4k Golang : Use NLP to get sentences for each paragraph example
+5.1k Golang : How to stop user from directly running an executable file?
+3.3k Google : Block or disable caching of your website content
+7.5k Golang : Characters limiter example