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
+23.2k Golang : How to verify uploaded file is image or allowed file types
+4.8k Golang : Create zip/ePub file without compression(use Store algorithm)
+12.7k Golang : Get download file size
+5.1k Golang : File system scanning
+9.2k Golang : Convert spaces to tabs and back to spaces example
+5.9k Golang : Gaussian blur on image and camera video feed examples
+3.1k Javascript : Change page title to get viewer attention
+23.2k Golang : Encrypt and decrypt data with AES crypto
+11.8k Golang : Set up source IP address before making HTTP request
+4.6k Golang : Null and nil value
+7.3k Golang : How to protect your source code from client, hosting company or hacker?
+21.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface