example_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package publicsuffix_test
  5. import (
  6. "fmt"
  7. "strings"
  8. "golang.org/x/net/publicsuffix"
  9. )
  10. // This example demonstrates looking up several domains' eTLDs (effective Top
  11. // Level Domains) in the PSL (Public Suffix List) snapshot. For each eTLD, the
  12. // example also determines whether the eTLD is ICANN managed, privately
  13. // managed, or unmanaged (not explicitly in the PSL).
  14. //
  15. // See https://publicsuffix.org/ for the underlying PSL data.
  16. func ExamplePublicSuffix_manager() {
  17. domains := []string{
  18. "amazon.co.uk",
  19. "books.amazon.co.uk",
  20. "www.books.amazon.co.uk",
  21. "amazon.com",
  22. "",
  23. "example0.debian.net",
  24. "example1.debian.org",
  25. "",
  26. "golang.dev",
  27. "golang.net",
  28. "play.golang.org",
  29. "gophers.in.space.museum",
  30. "",
  31. "0emm.com",
  32. "a.0emm.com",
  33. "b.c.d.0emm.com",
  34. "",
  35. "there.is.no.such-tld",
  36. "",
  37. // Examples from the PublicSuffix function's documentation.
  38. "foo.org",
  39. "foo.co.uk",
  40. "foo.dyndns.org",
  41. "foo.blogspot.co.uk",
  42. "cromulent",
  43. }
  44. for _, domain := range domains {
  45. if domain == "" {
  46. fmt.Println(">")
  47. continue
  48. }
  49. eTLD, icann := publicsuffix.PublicSuffix(domain)
  50. // Only ICANN managed domains can have a single label. Privately
  51. // managed domains must have multiple labels.
  52. manager := "Unmanaged"
  53. if icann {
  54. manager = "ICANN Managed"
  55. } else if strings.IndexByte(eTLD, '.') >= 0 {
  56. manager = "Privately Managed"
  57. }
  58. fmt.Printf("> %24s%16s is %s\n", domain, eTLD, manager)
  59. }
  60. // Output:
  61. // > amazon.co.uk co.uk is ICANN Managed
  62. // > books.amazon.co.uk co.uk is ICANN Managed
  63. // > www.books.amazon.co.uk co.uk is ICANN Managed
  64. // > amazon.com com is ICANN Managed
  65. // >
  66. // > example0.debian.net debian.net is Privately Managed
  67. // > example1.debian.org org is ICANN Managed
  68. // >
  69. // > golang.dev dev is ICANN Managed
  70. // > golang.net net is ICANN Managed
  71. // > play.golang.org org is ICANN Managed
  72. // > gophers.in.space.museum space.museum is ICANN Managed
  73. // >
  74. // > 0emm.com com is ICANN Managed
  75. // > a.0emm.com a.0emm.com is Privately Managed
  76. // > b.c.d.0emm.com d.0emm.com is Privately Managed
  77. // >
  78. // > there.is.no.such-tld such-tld is Unmanaged
  79. // >
  80. // > foo.org org is ICANN Managed
  81. // > foo.co.uk co.uk is ICANN Managed
  82. // > foo.dyndns.org dyndns.org is Privately Managed
  83. // > foo.blogspot.co.uk blogspot.co.uk is Privately Managed
  84. // > cromulent cromulent is Unmanaged
  85. }