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.1k Golang : Qt splash screen with delay example
+11.6k Golang : How to calculate the distance between two coordinates using Haversine formula
+5.6k Golang : How to determine if request or crawl is from Google robots
+11.5k Golang : Exit, terminating or aborting a program
+20k Golang : How to get time zone and load different time zone?
+3.9k Python : Find out the variable type and determine the type with simple test
+38.3k Golang : UDP client server read write example
+6.8k Javascript : Put image into Chrome browser's console
+21.8k Golang : Randomly pick an item from a slice/array example
+15.5k Golang : File path independent of Operating System
+6.4k Golang : Scanf function weird error in Windows
+4.8k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday