https://github.com/macrat/go-parallel-pinger
A easy and thread-safe way to send ping in Go.
https://github.com/macrat/go-parallel-pinger
Last synced: 5 months ago
JSON representation
A easy and thread-safe way to send ping in Go.
- Host: GitHub
- URL: https://github.com/macrat/go-parallel-pinger
- Owner: macrat
- License: mit
- Created: 2021-04-29T09:28:24.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-09T07:10:35.000Z (over 1 year ago)
- Last Synced: 2025-03-17T09:23:08.845Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-parallel-pinger
==================
[](https://pkg.go.dev/github.com/macrat/go-parallel-pinger)

[](https://github.com/macrat/go-parallel-pinger/actions/workflows/test.yml)
[](https://app.codecov.io/gh/macrat/go-parallel-pinger/)
A easy and thread-safe way to send ping in Go.
``` go
package main
import (
"context"
"log"
"net"
"time"
"github.com/macrat/go-parallel-pinger"
)
func Example() {
target, _ := net.ResolveIPAddr("ip", "127.0.0.1")
// 1. make Pinger for IPv4 (or, you can use NewIPv6)
p := pinger.NewIPv4()
// 2. make context for handle timeout or cancel
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// 3. start Pinger for send/receive ICMP packet before send ping
if err := p.Start(ctx); err != nil {
log.Fatalf("failed to start pinger: %s", err)
}
// 4. send ping for the target, and wait until receive all reply or context canceled
result, err := p.Ping(ctx, target, 4, 100*time.Millisecond)
if err != nil {
log.Fatalf("failed to send ping: %s", err)
}
// 5. check the result
log.Printf("sent %d packets and received %d packets", result.Sent, result.Recv)
log.Printf("RTT: min=%s / avg=%s / max=%s", result.MinRTT, result.AvgRTT, result.MaxRTT)
```