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