From 03865af4a238bc80e669968b909d3f9ed5aebfca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didrik=20Nordstr=C3=B6m?= Date: Wed, 28 Feb 2024 17:42:26 +0100 Subject: [PATCH] docs: use godoc examples --- client_test.go | 27 +++++++++++++++++++++++++++ service.go | 4 +--- service_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 service_test.go diff --git a/client_test.go b/client_test.go index 438b9f4..37873aa 100644 --- a/client_test.go +++ b/client_test.go @@ -2,6 +2,7 @@ package zeroconf import ( "context" + "fmt" "testing" "time" ) @@ -67,3 +68,29 @@ func TestBasic(t *testing.T) { func TestBasicSubtype(t *testing.T) { testBasic(t, testSubService) } + +func Example() { + // Browse for AirPlay devices on the local network + ty := NewType("_airplay._tcp") + zc, _ := New().Browse(func(e Event) { + fmt.Println(e) + }, ty).Open() + + defer zc.Close() + + // Main app logic +} + +func Example_pubsub() { + // Publish a service and browse for others of the same type + ty := NewType("_chat._tcp") + svc := NewService(ty, "bobs-laptop", 12345) + zc, _ := New(). + Publish(svc). + Browse(func(e Event) { + fmt.Println(e) + }).Open() + defer zc.Close() + + // Main app logic +} diff --git a/service.go b/service.go index 82b199c..d8cc4da 100644 --- a/service.go +++ b/service.go @@ -33,9 +33,7 @@ type Type struct { // Returns a type based on a string on the form `_my-service._tcp` or `_my-service._udp`. // // The domain is `local` by default, but can be specified explicitly. Finally, a comma- -// separated list of subtypes can be added at the end. Here is a full example: -// -// `_my-service._tcp.custom.domain,_printer,_sub1,_sub2` +// separated list of subtypes can be added at the end. func NewType(t string) Type { typeParts := strings.Split(t, ",") ty := Type{ diff --git a/service_test.go b/service_test.go new file mode 100644 index 0000000..3b4d1ae --- /dev/null +++ b/service_test.go @@ -0,0 +1,29 @@ +package zeroconf + +import "fmt" + +func ExampleNewType() { + ty := NewType("_chat._tcp") + fmt.Println(ty) + // Output: _chat._tcp.local +} + +func ExampleNewType_custom() { + // Provides a custom domain and two subtypes (not recommended) + ty := NewType("_foo._udp.custom.domain,_sub1,_sub2") + fmt.Println(ty) + fmt.Println(ty.Subtypes) + + // Output: + // _foo._udp.custom.domain + // [_sub1 _sub2] +} + +func ExampleNewService() { + ty := NewType("_chat._tcp") + svc := NewService(ty, "bobs-laptop", 12345) + fmt.Println(svc) + + // Output: + // bobs-laptop._chat._tcp.local +}