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
+14.9k Golang : How to check for empty array string or string?
+5.2k Responsive Google Adsense
+11.5k Golang : Concatenate (combine) buffer data example
+6.4k Golang : Test input string for unicode example
+13.9k Golang : Convert spaces to tabs and back to spaces example
+8.8k Golang : On lambda, anonymous, inline functions and function literals
+5.1k Golang : Check if a word is countable or not
+6.9k Golang : Normalize email to prevent multiple signups example
+26.7k Golang : Encrypt and decrypt data with AES crypto
+15.6k Golang : Force download file example
+5.9k Golang : List all packages and search for certain package
+4.6k Javascript : Detect when console is activated and do something about it