Skip to content

Commit

Permalink
docs: use godoc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
betamos committed Feb 28, 2024
1 parent 22f37e1 commit 03865af
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
27 changes: 27 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zeroconf

import (
"context"
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
29 changes: 29 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 03865af

Please sign in to comment.