Skip to content

Commit

Permalink
add testable example
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed May 30, 2022
1 parent 682f89d commit fadb323
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions checkip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package checkip

import (
"encoding/json"
"fmt"
"log"
"net"
)

// WellKnown implements checkip.Info.
type WellKnown bool

func (wk WellKnown) Json() ([]byte, error) {
return json.Marshal(wk)
}

func (wk WellKnown) Summary() string {
return fmt.Sprintf("%v", wk)
}

// IsWellKnown implements checkip.Check.
func IsWellKnown(ipaddr net.IP) (Result, error) {
res := Result{Name: "well known"}

wellKnown := []net.IP{
net.ParseIP("1.1.1.1"),
net.ParseIP("4.4.4.4"),
net.ParseIP("8.8.8.8"),
}

for _, wk := range wellKnown {
if string(ipaddr) == string(wk) {
res.Info = WellKnown(true)
}
}

return res, nil
}

func Example() {
ipaddr := net.ParseIP("1.1.1.1")
result, err := IsWellKnown(ipaddr)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output: {well known Info false true}
}

0 comments on commit fadb323

Please sign in to comment.