๐Ÿ“ฆ huangsam / namigo

๐Ÿ“„ util.go ยท 30 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30// Package core provides utility functions for the application.
package core

// dismiss ignores the error from a function/method
func dismiss(f func() error) {
	_ = f()
}

// IsValidDomainName validates a domain name for DNS lookups.
func IsValidDomainName(name string) bool {
	if len(name) == 0 || len(name) > 63 {
		return false
	}

	// Check each character
	for i, char := range name {
		// Must be alphanumeric or hyphen
		if (char < 'a' || char > 'z') && (char < 'A' || char > 'Z') &&
			(char < '0' || char > '9') && char != '-' {
			return false
		}
		// Cannot start or end with hyphen
		if char == '-' && (i == 0 || i == len(name)-1) {
			return false
		}
	}

	return true
}