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
+36k Golang : Save image to PNG, JPEG or GIF format.
+8.7k Golang : Gorilla web tool kit schema example
+5.6k Get website traffic ranking with Similar Web or Alexa
+10.1k Golang : How to profile or log time spend on execution?
+7.3k Golang : Word limiter example
+6.5k Golang : Embedded or data bundling example
+24.4k Golang : Time slice or date sort and reverse sort example
+20.6k Golang : Saving private and public key to files
+33.9k Golang : Create x509 certificate, private and public keys
+9.3k Golang : Scramble and unscramble text message by randomly replacing words
+8.9k Golang : Get SPF and DMARC from email headers to fight spam
+7.3k Golang : Scanf function weird error in Windows