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
+32.4k Golang : Regular Expression for alphanumeric and underscore
+6.6k Golang : Muxing with Martini example
+22.3k Golang : Set and Get HTTP request headers example
+7.3k Golang : How to handle file size larger than available memory panic issue
+9k Golang : How to get garbage collection data?
+6.5k Golang : Derive cryptographic key from passwords with Argon2
+11.8k Golang : Perform sanity checks on filename example
+5.4k Golang : Detect words using using consecutive letters in a given string
+20.5k PHP : Convert(cast) int to double/float
+11.3k Golang : Simple file scaning and remove virus example
+7.3k Android Studio : AlertDialog to get user attention example
+14k Golang : Convert IP version 6 address to integer or decimal number