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
+10k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+5.7k Golang : Use NLP to get sentences for each paragraph example
+10.6k Golang : How to transmit update file to client by HTTP request example
+5.7k PHP : Get client IP address
+8.7k Golang : Build and compile multiple source files
+5.6k Facebook : How to force facebook to scrape latest URL link data?
+4k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+6.4k Golang : Experimental emojis or emoticons icons programming language
+10.7k Golang : Simple image viewer with Go-GTK
+7.2k Golang : How to stop user from directly running an executable file?
+24.6k Golang : Generate MD5 checksum of a file
+18.2k Golang : Write file with io.WriteString