Categorygithub.com/macrat/go-parallel-pinger
modulepackage
1.1.5
Repository: https://github.com/macrat/go-parallel-pinger.git
Documentation: pkg.go.dev

# README

go-parallel-pinger

Documents Supports Linux, Darwin, and Windows GitHub Actions CI Status Codecov Test Coverage

A easy and thread-safe way to send ping in 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)

# Functions

NewIPv4 makes new Pinger for IPv4 protocol.
NewIPv6 makes new Pinger for IPv6 protocol.

# Constants

DEFAULT_PRIVILEGED is the default value of Pinger.Privileged/Pinger.SetPrivileged.

# Variables

ErrAlreadyStarted is the error if the pinger is already started.
ErrNotStarted is the error if call Pinger.Ping before start the pinger.

# Structs

Pinger is the ping sender and receiver.
Result is a result of Ping.