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
+8k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+5.9k Golang : Test input string for unicode example
+5.6k Golang : Extract unicode string from another unicode string example
+12k Golang : Remove or trim extra comma from CSV
+14.2k Golang : Missing Bazaar command
+9.6k Golang : Compare files modify date example
+4.8k Golang : Calculate half life decay example
+15.9k Golang : Check if a string contains multiple sub-strings in []string?
+7.4k Golang : Scan files for certain pattern and rename part of the files
+26.2k Golang : How to check if a connection to database is still alive ?
+8.1k Golang : Another camera capture GUI application with GTK and OpenCV