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
+16.9k Golang : How to save log messages to file?
+11k Golang : How to determine a prime number?
+16.3k Golang :Trim white spaces from a string
+6k PHP : Get client IP address
+4.8k Golang : A program that contain another program and executes it during run-time
+7.3k Golang : Word limiter example
+9.3k Golang : Find the length of big.Int variable example
+15k Golang : package is not in GOROOT during compilation
+6.5k Golang : Warp text string by number of characters or runes example
+22.6k Golang : Strings to lowercase and uppercase example
+15.2k Golang : Get all local users and print out their home directory, description and group id