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
+4.5k Golang : Debug with Godebug
+14.3k Golang : Get own process identifier
+3.4k Unix/Linux : How to pipe/save output of a command to file?
+9.3k Golang : Surveillance with web camera and OpenCV
+17.7k Golang : Encrypt and decrypt data with TripleDES
+4.4k Android Studio : Hello World example
+8.9k Golang : Create S3 bucket with official aws-sdk-go package
+4.7k Golang : Skip or discard items of non-interest when iterating example
+5.2k Golang : Example of custom handler for Gorilla's Path usage.
+13.7k Golang : Aligning strings to right, left and center with fill example
+3.3k Golang : Check if a word is countable or not
+4.9k Linux : How to fix Brother HL-1110 printing blank page problem