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
+8k Golang : How to use Gorilla webtoolkit context package properly
+5.7k Golang : Derive cryptographic key from passwords with Argon2
+8.3k Golang : Detect number of active displays and the display's resolution
+41.1k Golang : Get hardware information such as disk, memory and CPU usage
+25.4k Golang : Force your program to run with root permissions
+7.1k Findstr command the Grep equivalent for Windows
+28.1k Golang : missing Git command
+13k Golang : Google Drive API upload and rename example
+6.1k Golang : Accessing dataframe-go element by row, column and name example
+11.5k Golang : zlib compress file example
+7.2k Golang : Reverse text lines or flip line order example
+4.8k Javascript : How to refresh page with JQuery ?