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
+11.9k Golang : Verify Linux user password again before executing a program example
+12.9k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+7k Golang : Calculate BMI and risk category
+9.4k Golang : Temperatures conversion example
+5.7k PHP : Convert CSV to JSON with YQL example
+9.5k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+9.2k Golang : Gonum standard normal random numbers example
+8.5k Your page has meta tags in the body instead of the head
+13.9k Golang : Image to ASCII art example
+5.4k JavaScript/JQuery : Redirect page examples
+7.2k Golang : A simple forex opportunities scanner
+6.8k Golang : How to determine if request or crawl is from Google robots