move examples into a godoc-compatible Examples function

This commit is contained in:
mappu 2016-11-29 19:40:08 +13:00
parent 482b0d3ad8
commit ea97afb01f
3 changed files with 60 additions and 76 deletions

60
Example_test.go Normal file
View File

@ -0,0 +1,60 @@
package libnmdc
import (
"fmt"
)
func ExampleConnect() {
opts := HubConnectionOptions{
Address: "127.0.0.1",
Self: *NewUserInfo("slowpoke9"),
}
hub := opts.Connect()
for {
event := <-hub.OnEvent
switch event.EventType {
case EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
}
func ExampleConnectSync() {
cb := func(hub *HubConnection, event HubEvent) {
switch event.EventType {
case EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
opts := HubConnectionOptions{
Address: "127.0.0.1",
Self: *NewUserInfo("slowpoke9"),
OnEventSync: cb,
}
opts.ConnectSync() // blocking
}

View File

@ -1,39 +0,0 @@
// +build ignore
// This sample file demonstrates use of the libnmdc.go library. It's excluded
// when building the library package, but you can run it via `go run libnmdc_sample.go`.
// This version demonstrates the channel API.
package main
import (
"fmt"
"libnmdc"
)
func main() {
opts := libnmdc.HubConnectionOptions{
Address: "127.0.0.1",
Self: libnmdc.NewUserInfo("slowpoke9"),
}
hub := opts.Connect()
for {
event := <-hub.OnEvent
switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case libnmdc.EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
}

View File

@ -1,37 +0,0 @@
// +build ignore
// This sample file demonstrates use of the libnmdc.go library. It's excluded
// when building the library package, but you can run it via `go run libnmdc_sample.go`.
// This version demonstrates the sync API.
package main
import (
"fmt"
"libnmdc"
)
func main() {
libnmdc.HubConnectionOptions{
Address: "127.0.0.1",
Self: libnmdc.NewUserInfo("slowpoke9"),
OnEventSync: func(ev libnmdc.HubEvent) {
switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case libnmdc.EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
},
}.Connect()
}