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
+7.6k Golang : Getting Echo framework StartAutoTLS to work
+5.8k Javascript : Get operating system and browser information
+7.2k Golang : Check to see if *File is a file or directory
+7.6k Swift : Convert (cast) String to Double
+7.1k Golang : Of hash table and hash map
+17.2k Golang : Get future or past hours, minutes or seconds
+10.8k Golang : Generate random elements without repetition or duplicate
+23.5k Golang : Fix type interface{} has no field or no methods and type assertions example
+4.5k Facebook : How to place save to Facebook button on your website
+14k Golang : Convert IP version 6 address to integer or decimal number
+4.5k Adding Skype actions such as call and chat into web page examples
+4.7k Nginx and PageSpeed build from source CentOS example