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
+29.3k Golang : Interpolating or substituting variables in string examples
+6.9k Golang : Configure Apache and NGINX to access your Go service example
+4.9k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+19.6k Golang : Create and resolve(read) symbolic links
+15.1k Golang :Trim white spaces from a string
+8.1k Golang : Create and shuffle deck of cards example
+8.5k Golang : interface - when and where to use examples
+9.9k Android Studio : Create custom icons for your application example
+5.8k Default cipher that OpenSSL used to encrypt a PEM file
+5.6k Golang : How to determine if request or crawl is from Google robots
+16.1k Golang : Multi threading or run two processes or more example