Golang : How to convert(cast) string to IP address?
Problem :
You have IP address in string type and you want to convert(cast) the string into type IP address.
Solution :
Use the net.ParseIP()
function to convert the string to type IP address (http://golang.org/pkg/net/#IP)
For example :
package main
import (
"fmt"
"net"
)
func main() {
// type string
str := "106.10.138.240"
// type IP
IPAddress := net.ParseIP(str)
fmt.Println("4-byte representation : ", IPAddress.To4())
// fmt.Println("16-byte representation : ", IPAddress.To16())
}
See also : Golang : How to convert(cast) IP address to string?
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
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+4.9k Which content-type(MIME type) to use for JSON data
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+62.8k Golang : Convert HTTP Response body to string
+9.7k Golang : How to extract video or image files from html source code
+8.4k Golang: Prevent over writing file with md5 hash
+5.4k PHP : Hide PHP version information from curl
+9k Golang : Accept any number of function arguments with three dots(...)
+12.5k Golang : Extract part of string with regular expression
+6.4k Javascript : Generate random key with specific length
+10.2k Golang : Check a web page existence with HEAD request example
+4.7k Linux : sudo yum updates not working