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
+32.9k Delete a directory in Go
+22.4k Generate checksum for a file in Go
+6.4k Golang : Calculate diameter, circumference, area, sphere surface and volume
+13.3k Golang : Get constant name from value
+20.7k Golang : Underscore or snake_case to camel case example
+19.7k Golang : Append content to a file
+4.7k Golang : A program that contain another program and executes it during run-time
+7.8k Javascript : How to check a browser's Do Not Track status?
+19k Mac OSX : Homebrew and Golang
+9.9k Golang : Read file and convert content to string
+4.6k MariaDB/MySQL : Form select statement or search query with Chinese characters