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
+6.8k Golang : Pat multiplexer routing example
+5.6k Unix/Linux : How to find out the hard disk size?
+8.2k Golang : Check if integer is power of four example
+9.4k Mac OSX : Get a process/daemon status information
+8k Golang : Get all countries phone codes
+14.7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+5.7k Javascript : How to replace HTML inside <div>?
+20.1k Golang : How to get own program name during runtime ?
+36.3k Golang : Save image to PNG, JPEG or GIF format.
+11.2k Golang : How to use if, eq and print properly in html template
+14.5k Golang : Send email with attachment(RFC2822) using Gmail API example
+12k Golang : Sort and reverse sort a slice of runes