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
+6.7k Golang : Example of how to detect which type of script a word belongs to
+3.7k Java : Generate multiplication table example
+28.2k Golang : Save map/struct to JSON or XML file
+16.7k Golang : Read data from config file and assign to variables
+16.5k Golang : delete and modify XML file content
+5.9k Golang : Humanize and Titleize functions
+5.2k Golang : Detect variable or constant type
+7.6k Golang: Prevent over writing file with md5 hash
+4.7k Golang : Detect words using using consecutive letters in a given string
+19.6k PHP : Convert(cast) int to double/float
+6.1k Golang : Fixing Gorilla mux http.FileServer() 404 problem