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
+19.1k Golang : Fix cannot download, $GOPATH not set error
+7.8k Golang : Sort words with first uppercase letter
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+6k Linux/Unix : Commands that you need to be careful about
+9.1k Golang : Timeout example
+9.2k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+6.5k Golang : Experimental emojis or emoticons icons programming language
+9.6k Golang : Get current, epoch time and display by year, month and day
+22.6k Golang : simulate tail -f or read last line from log file example
+5.7k Golang : Shuffle array of list
+5.7k Golang : List all packages and search for certain package
+16.5k Golang : Gzip file example