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
+13.1k Golang : Count number of runes in string
+5.7k Golang : Extract unicode string from another unicode string example
+29.2k Golang : Login(Authenticate) with Facebook example
+21.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+20.4k Golang : Saving private and public key to files
+4.8k Golang : micron to centimeter example
+8.1k Golang: Prevent over writing file with md5 hash
+12.5k Golang : Convert int(year) to time.Time type
+5.3k Golang : Frobnicate or tweaking a string example
+26.4k Golang : How to check if a connection to database is still alive ?
+4.8k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version