summaryrefslogtreecommitdiff
path: root/packet.go
blob: 5b3a5baa9241920e0729b1dd3342e7a6d1e0bf3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
	"fmt"
	"net/url"
)

// packet accrues data as it passes through our concurrent
// pipeline. Formerly the web crawler only transmitted [url.URL]'s,
// but usingn a compound data type allows us to add URL
// depth-tracking.
type packet struct {
	url   url.URL
	depth int
}

// String implements the Stringer interface. We need this mainly
// because a [url.URL]'s String method only works when that URL is a
// pointer.
func (p packet) String() string {
	return fmt.Sprintf("[%d] %s", p.depth, &p.url)
}

// convertToPackets converts the batch of URLs to a slice of packet
// structs, configuring each one with the given depth.
func convertToPackets(batch []url.URL, depth int) []packet {
	var ps []packet

	for _, u := range batch {
		newPacket := packet{u, depth}
		ps = append(ps, newPacket)
	}

	return ps
}