Golang : Validate IP address
Problem :
You need to validate if a given IP address string is a valid IP address.
Solution :
Use regexp.Compile
function to validate the string.
For example :
package main
import (
"fmt"
"regexp"
"strings"
)
func validIP4(ipAddress string) bool {
ipAddress = strings.Trim(ipAddress, " ")
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
if re.MatchString(ipAddress) {
return true
}
return false
}
// NOTE : ip6 regular expression validation caused runtime error
// runtime error: invalid memory address or nil pointer dereference
//func validIP6(ip6Address string) bool {
// ip6Address = strings.Trim(ip6Address, " ")
// re, _ := regexp.Compile(`([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`)
// if re.MatchString(ip6Address) {
// return true
// }
// return false
//}
func main() {
ip1 := "192.168.0.0"
vIP1 := validIP4(ip1)
fmt.Printf("%s is a valid IP4 address : %v \n", ip1, vIP1)
//-----------------------------
ip2 := "192.168.a.0"
vIP2 := validIP4(ip2)
fmt.Printf("%s is a valid IP4 address : %v \n", ip2, vIP2)
//-----------------------------
fullIP6 := "FE80:0000:0000:0000:0202:B3FF:FE1E:8329"
vfullIP6 := validIP4(fullIP6)
fmt.Printf("%s is a valid IP4 address : %v \n", fullIP6, vfullIP6)
//-----------------------------
collapsedIP6 := "FE80::0202:B3FF:FE1E:8329"
vcollapsedIP6 := validIP4(collapsedIP6)
fmt.Printf("%s is a valid IP4 address : %v \n", collapsedIP6, vcollapsedIP6)
//-----------------------------
urlIP6 := "http://[2001:db8:0:1]:80"
vurlIP6 := validIP4(urlIP6)
fmt.Printf("%s is a valid IP4 address : %v \n", urlIP6, vurlIP6)
}
Output :
192.168.0.0 is a valid IP4 address : true
192.168.a.0 is a valid IP4 address : false
FE80:0000:0000:0000:0202:B3FF:FE1E:8329 is a valid IP4 address : false
FE80::0202:B3FF:FE1E:8329 is a valid IP4 address : false
http://[2001:db8:0:1]:80 is a valid IP4 address : false
NOTE : I'm not able to get the right regular expression to validate ip6 address yet. If you have a solution, please leave a comment below. :-)
UPDATE : You can also use net.ParseIP()
instead of regular expression to validate a given IP address. Except IP version 6 address with URL or port number.
var ipAddress string = "192.168.0.1"
//sanity check
testInput := net.ParseIP(ipAddress)
if testInput.To4() == nil {
fmt.Printf("%v is not a valid IPv4 address\n", testInput)
}
or
var ip6Address string = "FE80::0202:B3FF:FE1E:8329"
//sanity check
testInputIP6 := net.ParseIP(ip6Address)
if testInputIP6.To16() == nil {
fmt.Printf("%v is not a valid IP address\n", testInputIP6)
}
Reference :
See also : Golang : Validate hostname
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
+20.2k Golang : Underscore or snake_case to camel case example
+18k Golang : Display list of time zones with GMT
+19.3k Golang : How to get own program name during runtime ?
+17.2k Golang : Upload/Receive file progress indicator
+11.5k Golang : Convert decimal number(integer) to IPv4 address
+6.5k Golang : Gargish-English language translator
+5.6k Golang : Shuffle array of list
+10.6k Golang : Create S3 bucket with official aws-sdk-go package
+11.1k Golang : Concurrency and goroutine example
+11k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+7.3k Golang : Lock executable to a specific machine with unique hash of the machine
+5.3k Linux/Unix/PHP : Restart PHP-FPM