How can I check whether URL.Hostname is a valid domain name? I am searching for the exact same behaviour as URI.CheckHostName in the .NET framework.
Asked
Active
Viewed 1.4k times
5
-
2Currently, there is no solution using standard library only. You need to use third party library/regex. However, if you need to support *internationalized domain name*, the task may not trivial. See [https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation](https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation), [https://stackoverflow.com/questions/3523028/valid-characters-of-a-hostname](https://stackoverflow.com/questions/3523028/valid-characters-of-a-hostname) – putu Jan 13 '18 at 11:14
4 Answers
4
You could use idna, kind of, to check on this.
import "golang.org/x/net/idna"
h, err := idna.Lookup.ToASCII("example.com")
fmt.Println(h, err)
Also see;

Komu
- 14,174
- 2
- 28
- 22
0
The function IsDNSName from the govalidator
package should do what you want. For details on installing it, see the README.

phlummox
- 234
- 5
- 11
-
1It allows underscore in domain names which is wrong. See RFC1034 section 3.5. A later change to this RFC is to allow digits as first label character. The TLD label can't start with a digit to avoid confusion between domain names and IPv4 addresses. – chmike Jun 26 '18 at 08:16
0
Here you have. https://pkg.go.dev/github.com/dchest/validator
func ValidateDomainByResolvingIt(domain string) error ValidateDomainByResolvingIt queries DNS for the given domain name, and returns nil if the the name resolves, or an error.

raaa
- 1
- 1