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
+9.2k Golang : Capture text return from exec function example
+19.5k Golang : Check whether a network interface is up on your machine
+10.2k Golang : Test a slice of integers for odd and even numbers
+8.1k Golang : Grayscale Image
+5.1k Python : Find out the variable type and determine the type with simple test
+8.1k Swift : Convert (cast) String to Float
+22.1k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+15k Golang : Get URI segments by number and assign as variable example
+9k Golang : Executing and evaluating nested loop in html template
+14k Golang : Convert spaces to tabs and back to spaces example