func isValidDomainName(hostname string) error {
	previous := rune('.')
	nonNumeric := false
	segmentLen := 0
	for _, char := range hostname {
		switch {
		case '0' <= char && char <= '9':
			segmentLen++
		case 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' || char == '_':
			nonNumeric = true
			segmentLen++
		case char == '-':
			if previous == '.' {
				return fmt.Errorf("cannot begin a segment after a period (.) with a hyphen (-)")
			}
			nonNumeric = true
			segmentLen++
		case char == '.':
			if previous == '.' {
				return fmt.Errorf("cannot contain two periods (.) in a row")
			}
			if previous == '-' {
				return fmt.Errorf("cannot have a hyphen (-) immediately before a period (.)")
			}
			if segmentLen > maxSegmentLength {
				return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
			}
			segmentLen = 0
		default:
			return fmt.Errorf("included invalid character %q, must only contain letters, digits, periods (.), hyphens (-), or underscores (_)", char)
		}
		previous = char
	}

	if previous == '-' {
		return fmt.Errorf("cannot have a hyphen (-) as the final character")
	}
	if segmentLen > maxSegmentLength {
		return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
	}
	if !nonNumeric {
		return errors.New("must have at least one non-numeric character")
	}

	return nil
}
// isValidDomainName validates a hostname according to the requirements set for
// domain names internally in the Go standard library's net package, see
// golang.org/issue/12421.
//
// Adapted from https://github.com/golang/go/blob/f4e7a6b905ce60448e506a3f6578d01b60602cdd/src/net/dnsclient.go#L73-L128
// See https://github.com/golang/go/blob/f4e7a6b905ce60448e506a3f6578d01b60602cdd/LICENSE for the license.
isValidDomainName validates a hostname according to the requirements set for
domain names internally in the Go standard library's net package, see
golang.org/issue/12421.


// isValidDomainName validates a hostname according to the requirements set for // domain names internally in the Go standard library ' s net package see // golang . org / issue / 12421 . // // Adapted from https : // github . com / golang / go / blob / f4e7a6b905ce60448e506a3f6578d01b60602cdd / src / net / dnsclient . go#L73 - L128 // See https : // github . com / golang / go / blob / f4e7a6b905ce60448e506a3f6578d01b60602cdd / LICENSE for the license .
func isValidDomainName(hostname string) error {
	previous := rune('.')
	nonNumeric := false
	segmentLen := 0
	for _, char := range hostname {
		switch {
		case '0' <= char && char <= '9':
			segmentLen++
		case 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' || char == '_':
			nonNumeric = true
			segmentLen++
		case char == '-':
			if previous == '.' {
				return fmt.Errorf("cannot begin a segment after a period (.) with a hyphen (-)")
			}
			nonNumeric = true
			segmentLen++
		case char == '.':
			if previous == '.' {
				return fmt.Errorf("cannot contain two periods (.) in a row")
			}
			if previous == '-' {
				return fmt.Errorf("cannot have a hyphen (-) immediately before a period (.)")
			}
			if segmentLen > maxSegmentLength {
				return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
			}
			segmentLen = 0
		default:
			return fmt.Errorf("included invalid character %q, must only contain letters, digits, periods (.), hyphens (-), or underscores (_)", char)
		}
		previous = char
	}
	if previous == '-' {
		return fmt.Errorf("cannot have a hyphen (-) as the final character")
	}
	if segmentLen > maxSegmentLength {
		return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
	}
	if !nonNumeric {
		return errors.New("must have at least one non-numeric character")
	}
	return nil
}
func isValidDomainName ( hostname string ) error { previous := rune ( '.' ) nonNumeric := false segmentLen := 0 for _ , char := range hostname { switch { case '0' <= char && char <= '9' : segmentLen ++ case 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' || char == '_' : nonNumeric = true segmentLen ++ case char == '-' : if previous == '.' { return fmt . Errorf ( " " ) } nonNumeric = true segmentLen ++ case char == '.' : if previous == '.' { return fmt . Errorf ( " " ) } if previous == '-' { return fmt . Errorf ( " " ) } if segmentLen > maxSegmentLength { return fmt . Errorf ( " " , maxSegmentLength ) } segmentLen = 0 default : return fmt . Errorf ( " " , char ) } previous = char } if previous == '-' { return fmt . Errorf ( " " ) } if segmentLen > maxSegmentLength { return fmt . Errorf ( " " , maxSegmentLength ) } if ! nonNumeric { return errors . New ( " " ) } return nil }
isValidDomainName validates a hostname according to the requirements set for
domain names internally in the Go standard library's net package, see
golang.org/issue/12421.
isValidDomainName validates a hostname according to the requirements set for domain names internally in the Go standard library ' s net package see golang . org / issue / 12421 .