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
+28.9k Golang : JQuery AJAX post data to server and send data back to client example
+30.8k Golang : bufio.NewReader.ReadLine to read file line by line
+7.5k Golang : Getting Echo framework StartAutoTLS to work
+5k Python : Convert(cast) string to bytes example
+6.9k CloudFlare : Another way to get visitor's real IP address
+33.9k Golang : Smarter Error Handling with strings.Contains()
+16.1k Golang : Send email and SMTP configuration example
+7.9k Golang : Emulate NumPy way of creating matrix example
+6.8k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+7k Golang : Not able to grep log.Println() output
+19.5k Golang : Append content to a file
+15.1k Golang : rune literal not terminated error