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
+48.2k Golang : Upload file from web browser to server
+26.5k Golang : Find files by extension
+7.7k Javascript : Put image into Chrome browser's console
+11.4k Golang : Calculations using complex numbers example
+7k Golang : How to detect if a sentence ends with a punctuation?
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+14.7k Golang : Save(pipe) HTTP response into a file
+16.4k Golang : Gzip file example
+7.6k Golang : Get today's weekday name and calculate target day distance example
+25.9k Golang : Get executable name behind process ID example
+23.3k Find and replace a character in a string in Go