Skip to content

Commit 2620430

Browse files
dsnetpull[bot]
authored andcommitted
encoding: modernize Go documentation
Across all encoding packages, linkify declarations if possible. In some cases, we convert a code block into a bulleted list, which then further allows for more linkification. Change-Id: I68fedf362615b34228bab5d4859b7d87d831c570 Reviewed-on: https://go-review.googlesource.com/c/go/+/524977 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: qiulaidongfeng <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent c99b4c0 commit 2620430

24 files changed

+204
-197
lines changed

src/encoding/ascii85/ascii85.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
* Encoder
1616
*/
1717

18-
// Encode encodes src into at most MaxEncodedLen(len(src))
18+
// Encode encodes src into at most [MaxEncodedLen](len(src))
1919
// bytes of dst, returning the actual number of bytes written.
2020
//
2121
// The encoding handles 4-byte chunks, using a special encoding
2222
// for the last fragment, so Encode is not appropriate for use on
23-
// individual blocks of a large data stream. Use NewEncoder() instead.
23+
// individual blocks of a large data stream. Use [NewEncoder] instead.
2424
//
2525
// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
2626
// Encode does not add these.
@@ -173,7 +173,7 @@ func (e CorruptInputError) Error() string {
173173
// Decode decodes src into dst, returning both the number
174174
// of bytes written to dst and the number consumed from src.
175175
// If src contains invalid ascii85 data, Decode will return the
176-
// number of bytes successfully written and a CorruptInputError.
176+
// number of bytes successfully written and a [CorruptInputError].
177177
// Decode ignores space and control characters in src.
178178
// Often, ascii85-encoded data is wrapped in <~ and ~> symbols.
179179
// Decode expects these to have been stripped by the caller.
@@ -182,7 +182,7 @@ func (e CorruptInputError) Error() string {
182182
// end of the input stream and processes it completely rather
183183
// than wait for the completion of another 32-bit block.
184184
//
185-
// NewDecoder wraps an io.Reader interface around Decode.
185+
// [NewDecoder] wraps an [io.Reader] interface around Decode.
186186
func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err error) {
187187
var v uint32
188188
var nb int

src/encoding/asn1/asn1.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func parseBitString(bytes []byte) (ret BitString, err error) {
211211

212212
// NULL
213213

214-
// NullRawValue is a RawValue with its Tag set to the ASN.1 NULL type tag (5).
214+
// NullRawValue is a [RawValue] with its Tag set to the ASN.1 NULL type tag (5).
215215
var NullRawValue = RawValue{Tag: TagNull}
216216

217217
// NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.
@@ -1031,34 +1031,33 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
10311031
// fields in val will not be included in rest, as these are considered
10321032
// valid elements of the SEQUENCE and not trailing data.
10331033
//
1034-
// An ASN.1 INTEGER can be written to an int, int32, int64,
1035-
// or *big.Int (from the math/big package).
1036-
// If the encoded value does not fit in the Go type,
1037-
// Unmarshal returns a parse error.
1034+
// - An ASN.1 INTEGER can be written to an int, int32, int64,
1035+
// or *[big.Int].
1036+
// If the encoded value does not fit in the Go type,
1037+
// Unmarshal returns a parse error.
10381038
//
1039-
// An ASN.1 BIT STRING can be written to a BitString.
1039+
// - An ASN.1 BIT STRING can be written to a [BitString].
10401040
//
1041-
// An ASN.1 OCTET STRING can be written to a []byte.
1041+
// - An ASN.1 OCTET STRING can be written to a []byte.
10421042
//
1043-
// An ASN.1 OBJECT IDENTIFIER can be written to an
1044-
// ObjectIdentifier.
1043+
// - An ASN.1 OBJECT IDENTIFIER can be written to an [ObjectIdentifier].
10451044
//
1046-
// An ASN.1 ENUMERATED can be written to an Enumerated.
1045+
// - An ASN.1 ENUMERATED can be written to an [Enumerated].
10471046
//
1048-
// An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time.
1047+
// - An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a [time.Time].
10491048
//
1050-
// An ASN.1 PrintableString, IA5String, or NumericString can be written to a string.
1049+
// - An ASN.1 PrintableString, IA5String, or NumericString can be written to a string.
10511050
//
1052-
// Any of the above ASN.1 values can be written to an interface{}.
1053-
// The value stored in the interface has the corresponding Go type.
1054-
// For integers, that type is int64.
1051+
// - Any of the above ASN.1 values can be written to an interface{}.
1052+
// The value stored in the interface has the corresponding Go type.
1053+
// For integers, that type is int64.
10551054
//
1056-
// An ASN.1 SEQUENCE OF x or SET OF x can be written
1057-
// to a slice if an x can be written to the slice's element type.
1055+
// - An ASN.1 SEQUENCE OF x or SET OF x can be written
1056+
// to a slice if an x can be written to the slice's element type.
10581057
//
1059-
// An ASN.1 SEQUENCE or SET can be written to a struct
1060-
// if each of the elements in the sequence can be
1061-
// written to the corresponding element in the struct.
1058+
// - An ASN.1 SEQUENCE or SET can be written to a struct
1059+
// if each of the elements in the sequence can be
1060+
// written to the corresponding element in the struct.
10621061
//
10631062
// The following tags on struct fields have special meaning to Unmarshal:
10641063
//

src/encoding/asn1/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
721721

722722
// Marshal returns the ASN.1 encoding of val.
723723
//
724-
// In addition to the struct tags recognised by Unmarshal, the following can be
724+
// In addition to the struct tags recognized by Unmarshal, the following can be
725725
// used:
726726
//
727727
// ia5: causes strings to be marshaled as ASN.1, IA5String values

src/encoding/base32/base32.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const (
5757
// The alphabet is treated as a sequence of byte values
5858
// without any special treatment for multi-byte UTF-8.
5959
// The resulting Encoding uses the default padding character ('='),
60-
// which may be changed or disabled via WithPadding.
60+
// which may be changed or disabled via [Encoding.WithPadding].
6161
func NewEncoding(encoder string) *Encoding {
6262
if len(encoder) != 32 {
6363
panic("encoding alphabet is not 32-bytes long")
@@ -112,12 +112,12 @@ func (enc Encoding) WithPadding(padding rune) *Encoding {
112112
* Encoder
113113
*/
114114

115-
// Encode encodes src using the encoding enc, writing
116-
// EncodedLen(len(src)) bytes to dst.
115+
// Encode encodes src using the encoding enc,
116+
// writing [Encoding.EncodedLen](len(src)) bytes to dst.
117117
//
118118
// The encoding pads the output to a multiple of 8 bytes,
119119
// so Encode is not appropriate for use on individual blocks
120-
// of a large data stream. Use NewEncoder() instead.
120+
// of a large data stream. Use [NewEncoder] instead.
121121
func (enc *Encoding) Encode(dst, src []byte) {
122122
if len(src) == 0 {
123123
return
@@ -386,10 +386,10 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
386386
}
387387

388388
// Decode decodes src using the encoding enc. It writes at most
389-
// DecodedLen(len(src)) bytes to dst and returns the number of bytes
389+
// [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes
390390
// written. If src contains invalid base32 data, it will return the
391-
// number of bytes successfully written and CorruptInputError.
392-
// New line characters (\r and \n) are ignored.
391+
// number of bytes successfully written and [CorruptInputError].
392+
// Newline characters (\r and \n) are ignored.
393393
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
394394
buf := make([]byte, len(src))
395395
l := stripNewlines(buf, src)

src/encoding/base64/base64.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const (
6060
// The alphabet is treated as a sequence of byte values
6161
// without any special treatment for multi-byte UTF-8.
6262
// The resulting Encoding uses the default padding character ('='),
63-
// which may be changed or disabled via WithPadding.
63+
// which may be changed or disabled via [Encoding.WithPadding].
6464
func NewEncoding(encoder string) *Encoding {
6565
if len(encoder) != 64 {
6666
panic("encoding alphabet is not 64-bytes long")
@@ -87,7 +87,7 @@ func NewEncoding(encoder string) *Encoding {
8787
}
8888

8989
// WithPadding creates a new encoding identical to enc except
90-
// with a specified padding character, or NoPadding to disable padding.
90+
// with a specified padding character, or [NoPadding] to disable padding.
9191
// The padding character must not be '\r' or '\n',
9292
// must not be contained in the encoding's alphabet,
9393
// must not be negative, and must be a rune equal or below '\xff'.
@@ -124,24 +124,24 @@ var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw
124124

125125
// RawStdEncoding is the standard raw, unpadded base64 encoding,
126126
// as defined in RFC 4648 section 3.2.
127-
// This is the same as StdEncoding but omits padding characters.
127+
// This is the same as [StdEncoding] but omits padding characters.
128128
var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
129129

130130
// RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
131131
// It is typically used in URLs and file names.
132-
// This is the same as URLEncoding but omits padding characters.
132+
// This is the same as [URLEncoding] but omits padding characters.
133133
var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
134134

135135
/*
136136
* Encoder
137137
*/
138138

139-
// Encode encodes src using the encoding enc, writing
140-
// EncodedLen(len(src)) bytes to dst.
139+
// Encode encodes src using the encoding enc,
140+
// writing [Encoding.EncodedLen](len(src)) bytes to dst.
141141
//
142142
// The encoding pads the output to a multiple of 4 bytes,
143143
// so Encode is not appropriate for use on individual blocks
144-
// of a large data stream. Use NewEncoder() instead.
144+
// of a large data stream. Use [NewEncoder] instead.
145145
func (enc *Encoding) Encode(dst, src []byte) {
146146
if len(src) == 0 {
147147
return
@@ -507,9 +507,9 @@ func (d *decoder) Read(p []byte) (n int, err error) {
507507
}
508508

509509
// Decode decodes src using the encoding enc. It writes at most
510-
// DecodedLen(len(src)) bytes to dst and returns the number of bytes
510+
// [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes
511511
// written. If src contains invalid base64 data, it will return the
512-
// number of bytes successfully written and CorruptInputError.
512+
// number of bytes successfully written and [CorruptInputError].
513513
// New line characters (\r and \n) are ignored.
514514
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
515515
if len(src) == 0 {

src/encoding/binary/binary.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
//
1818
// This package favors simplicity over efficiency. Clients that require
1919
// high-performance serialization, especially for large data structures,
20-
// should look at more advanced solutions such as the encoding/gob
21-
// package or protocol buffers.
20+
// should look at more advanced solutions such as the [encoding/gob]
21+
// package or [google.golang.org/protobuf] for protocol buffers.
2222
package binary
2323

2424
import (
@@ -31,6 +31,8 @@ import (
3131

3232
// A ByteOrder specifies how to convert byte slices into
3333
// 16-, 32-, or 64-bit unsigned integers.
34+
//
35+
// It is implemented by [LittleEndian], [BigEndian], and [NativeEndian].
3436
type ByteOrder interface {
3537
Uint16([]byte) uint16
3638
Uint32([]byte) uint32
@@ -43,17 +45,19 @@ type ByteOrder interface {
4345

4446
// AppendByteOrder specifies how to append 16-, 32-, or 64-bit unsigned integers
4547
// into a byte slice.
48+
//
49+
// It is implemented by [LittleEndian], [BigEndian], and [NativeEndian].
4650
type AppendByteOrder interface {
4751
AppendUint16([]byte, uint16) []byte
4852
AppendUint32([]byte, uint32) []byte
4953
AppendUint64([]byte, uint64) []byte
5054
String() string
5155
}
5256

53-
// LittleEndian is the little-endian implementation of ByteOrder and AppendByteOrder.
57+
// LittleEndian is the little-endian implementation of [ByteOrder] and [AppendByteOrder].
5458
var LittleEndian littleEndian
5559

56-
// BigEndian is the big-endian implementation of ByteOrder and AppendByteOrder.
60+
// BigEndian is the big-endian implementation of [ByteOrder] and [AppendByteOrder].
5761
var BigEndian bigEndian
5862

5963
type littleEndian struct{}
@@ -227,9 +231,9 @@ func (nativeEndian) GoString() string { return "binary.NativeEndian" }
227231
// When reading into a struct, all non-blank fields must be exported
228232
// or Read may panic.
229233
//
230-
// The error is EOF only if no bytes were read.
231-
// If an EOF happens after reading some but not all the bytes,
232-
// Read returns ErrUnexpectedEOF.
234+
// The error is [io.EOF] only if no bytes were read.
235+
// If an [io.EOF] happens after reading some but not all the bytes,
236+
// Read returns [io.ErrUnexpectedEOF].
233237
func Read(r io.Reader, order ByteOrder, data any) error {
234238
// Fast path for basic types and slices.
235239
if n := intDataSize(data); n != 0 {
@@ -460,7 +464,7 @@ func Write(w io.Writer, order ByteOrder, data any) error {
460464
return err
461465
}
462466

463-
// Size returns how many bytes Write would generate to encode the value v, which
467+
// Size returns how many bytes [Write] would generate to encode the value v, which
464468
// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
465469
// If v is neither of these, Size returns -1.
466470
func Size(v any) int {

src/encoding/binary/native_endian_big.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ type nativeEndian struct {
1010
bigEndian
1111
}
1212

13-
// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
13+
// NativeEndian is the native-endian implementation of [ByteOrder] and [AppendByteOrder].
1414
var NativeEndian nativeEndian

src/encoding/binary/native_endian_little.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ type nativeEndian struct {
1010
littleEndian
1111
}
1212

13-
// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
13+
// NativeEndian is the native-endian implementation of [ByteOrder] and [AppendByteOrder].
1414
var NativeEndian nativeEndian

src/encoding/binary/varint.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
)
3838

3939
// AppendUvarint appends the varint-encoded form of x,
40-
// as generated by PutUvarint, to buf and returns the extended buffer.
40+
// as generated by [PutUvarint], to buf and returns the extended buffer.
4141
func AppendUvarint(buf []byte, x uint64) []byte {
4242
for x >= 0x80 {
4343
buf = append(buf, byte(x)|0x80)
@@ -88,7 +88,7 @@ func Uvarint(buf []byte) (uint64, int) {
8888
}
8989

9090
// AppendVarint appends the varint-encoded form of x,
91-
// as generated by PutVarint, to buf and returns the extended buffer.
91+
// as generated by [PutVarint], to buf and returns the extended buffer.
9292
func AppendVarint(buf []byte, x int64) []byte {
9393
ux := uint64(x) << 1
9494
if x < 0 {
@@ -126,9 +126,9 @@ func Varint(buf []byte) (int64, int) {
126126
var errOverflow = errors.New("binary: varint overflows a 64-bit integer")
127127

128128
// ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.
129-
// The error is EOF only if no bytes were read.
130-
// If an EOF happens after reading some but not all the bytes,
131-
// ReadUvarint returns io.ErrUnexpectedEOF.
129+
// The error is [io.EOF] only if no bytes were read.
130+
// If an [io.EOF] happens after reading some but not all the bytes,
131+
// ReadUvarint returns [io.ErrUnexpectedEOF].
132132
func ReadUvarint(r io.ByteReader) (uint64, error) {
133133
var x uint64
134134
var s uint
@@ -153,9 +153,9 @@ func ReadUvarint(r io.ByteReader) (uint64, error) {
153153
}
154154

155155
// ReadVarint reads an encoded signed integer from r and returns it as an int64.
156-
// The error is EOF only if no bytes were read.
157-
// If an EOF happens after reading some but not all the bytes,
158-
// ReadVarint returns io.ErrUnexpectedEOF.
156+
// The error is [io.EOF] only if no bytes were read.
157+
// If an [io.EOF] happens after reading some but not all the bytes,
158+
// ReadVarint returns [io.ErrUnexpectedEOF].
159159
func ReadVarint(r io.ByteReader) (int64, error) {
160160
ux, err := ReadUvarint(r) // ok to continue in presence of error
161161
x := int64(ux >> 1)

src/encoding/csv/reader.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (e *ParseError) Error() string {
8282

8383
func (e *ParseError) Unwrap() error { return e.Err }
8484

85-
// These are the errors that can be returned in ParseError.Err.
85+
// These are the errors that can be returned in [ParseError.Err].
8686
var (
8787
ErrBareQuote = errors.New("bare \" in non-quoted-field")
8888
ErrQuote = errors.New("extraneous or missing \" in quoted-field")
@@ -100,9 +100,9 @@ func validDelim(r rune) bool {
100100

101101
// A Reader reads records from a CSV-encoded file.
102102
//
103-
// As returned by NewReader, a Reader expects input conforming to RFC 4180.
103+
// As returned by [NewReader], a Reader expects input conforming to RFC 4180.
104104
// The exported fields can be changed to customize the details before the
105-
// first call to Read or ReadAll.
105+
// first call to [Reader.Read] or [Reader.ReadAll].
106106
//
107107
// The Reader converts all \r\n sequences in its input to plain \n,
108108
// including in multiline field values, so that the returned data does
@@ -186,12 +186,12 @@ func NewReader(r io.Reader) *Reader {
186186

187187
// Read reads one record (a slice of fields) from r.
188188
// If the record has an unexpected number of fields,
189-
// Read returns the record along with the error ErrFieldCount.
189+
// Read returns the record along with the error [ErrFieldCount].
190190
// If the record contains a field that cannot be parsed,
191191
// Read returns a partial record along with the parse error.
192192
// The partial record contains all fields read before the error.
193-
// If there is no data left to be read, Read returns nil, io.EOF.
194-
// If ReuseRecord is true, the returned slice may be shared
193+
// If there is no data left to be read, Read returns nil, [io.EOF].
194+
// If [Reader.ReuseRecord] is true, the returned slice may be shared
195195
// between multiple calls to Read.
196196
func (r *Reader) Read() (record []string, err error) {
197197
if r.ReuseRecord {
@@ -205,7 +205,7 @@ func (r *Reader) Read() (record []string, err error) {
205205

206206
// FieldPos returns the line and column corresponding to
207207
// the start of the field with the given index in the slice most recently
208-
// returned by Read. Numbering of lines and columns starts at 1;
208+
// returned by [Reader.Read]. Numbering of lines and columns starts at 1;
209209
// columns are counted in bytes, not runes.
210210
//
211211
// If this is called with an out-of-bounds index, it panics.
@@ -231,7 +231,7 @@ type position struct {
231231

232232
// ReadAll reads all the remaining records from r.
233233
// Each record is a slice of fields.
234-
// A successful call returns err == nil, not err == io.EOF. Because ReadAll is
234+
// A successful call returns err == nil, not err == [io.EOF]. Because ReadAll is
235235
// defined to read until EOF, it does not treat end of file as an error to be
236236
// reported.
237237
func (r *Reader) ReadAll() (records [][]string, err error) {
@@ -249,7 +249,7 @@ func (r *Reader) ReadAll() (records [][]string, err error) {
249249

250250
// readLine reads the next line (with the trailing endline).
251251
// If EOF is hit without a trailing endline, it will be omitted.
252-
// If some bytes were read, then the error is never io.EOF.
252+
// If some bytes were read, then the error is never [io.EOF].
253253
// The result is only valid until the next call to readLine.
254254
func (r *Reader) readLine() ([]byte, error) {
255255
line, err := r.r.ReadSlice('\n')

0 commit comments

Comments
 (0)