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
+6.4k Golang : Mapping Iban to Dunging alphabets
+5.6k Golang : How to solve "too many .rsrc sections" error?
+5.8k Elasticsearch : Shutdown a local node
+5k Unix/Linux : How to test user agents blocked successfully ?
+2.3k Golang : Fix go-cron set time not working issue
+6.1k Golang : Calculate BMI and risk category
+3.9k JavaScript : Rounding number to decimal formats to display currency
+7k Golang : Scan files for certain pattern and rename part of the files
+6.8k SSL : How to check if current certificate is sha1 or sha2 from command line
+6.3k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+15.3k Golang : How to extract links from web page ?
+4.5k Golang : How to deal with configuration data?