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
+37.1k Upload multiple files with Go
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+11.4k Golang : Convert(cast) float to int
+9.4k Golang : Detect number of active displays and the display's resolution
+33.7k Golang : Call a function after some delay(time.Sleep and Tick)
+18.5k Unmarshal/Load CSV record into struct in Go
+26k Golang : Calculate future date with time.Add() function
+6.9k Golang : Gorrila mux.Vars() function example
+18.6k Golang : Delete duplicate items from a slice/array
+7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+21.5k Golang : How to reverse slice or array elements order
+9.4k Javascript : Read/parse JSON data from HTTP response