Skip to content

Commit 2a71af1

Browse files
mateusz834gopherbot
authored andcommitted
net/url: improve URL docs
The Raw fields are confusing and easy to use by mistake. Adds more context in comments to these fields. Also the current docs (and the names of these fields) of these boolean fields are not obvious that parser might produce them, so clarify that Change-Id: I6a6a69644834c3ccbf657147f771930b6875f721 Reviewed-on: https://go-review.googlesource.com/c/go/+/706515 Reviewed-by: Florian Lehner <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ee5369b commit 2a71af1

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/net/url/url.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,25 +364,41 @@ func escape(s string, mode encoding) string {
364364
// A consequence is that it is impossible to tell which slashes in the Path were
365365
// slashes in the raw URL and which were %2f. This distinction is rarely important,
366366
// but when it is, the code should use the [URL.EscapedPath] method, which preserves
367-
// the original encoding of Path.
367+
// the original encoding of Path. The Fragment field is also stored in decoded form,
368+
// use [URL.EscapedFragment] to retrieve the original encoding.
368369
//
369-
// The RawPath field is an optional field which is only set when the default
370-
// encoding of Path is different from the escaped path. See the EscapedPath method
371-
// for more details.
372-
//
373-
// URL's String method uses the EscapedPath method to obtain the path.
370+
// The [URL.String] method uses the [URL.EscapedPath] method to obtain the path.
374371
type URL struct {
375-
Scheme string
376-
Opaque string // encoded opaque data
377-
User *Userinfo // username and password information
378-
Host string // host or host:port (see Hostname and Port methods)
379-
Path string // path (relative paths may omit leading slash)
380-
RawPath string // encoded path hint (see EscapedPath method)
381-
OmitHost bool // do not emit empty host (authority)
382-
ForceQuery bool // append a query ('?') even if RawQuery is empty
383-
RawQuery string // encoded query values, without '?'
384-
Fragment string // fragment for references, without '#'
385-
RawFragment string // encoded fragment hint (see EscapedFragment method)
372+
Scheme string
373+
Opaque string // encoded opaque data
374+
User *Userinfo // username and password information
375+
Host string // "host" or "host:port" (see Hostname and Port methods)
376+
Path string // path (relative paths may omit leading slash)
377+
Fragment string // fragment for references (without '#')
378+
379+
// RawQuery contains the encoded query values, without the initial '?'.
380+
// Use URL.Query to decode the query.
381+
RawQuery string
382+
383+
// RawPath is an optional field containing an encoded path hint.
384+
// See the EscapedPath method for more details.
385+
//
386+
// In general, code should call EscapedPath instead of reading RawPath.
387+
RawPath string
388+
389+
// RawFragment is an optional field containing an encoded fragment hint.
390+
// See the EscapedFragment method for more details.
391+
//
392+
// In general, code should call EscapedFragment instead of reading RawFragment.
393+
RawFragment string
394+
395+
// ForceQuery indicates whether the original URL contained a query ('?') character.
396+
// When set, the String method will include a trailing '?', even when RawQuery is empty.
397+
ForceQuery bool
398+
399+
// OmitHost indicates the URL has an empty host (authority).
400+
// When set, the String method will not include the host when it is empty.
401+
OmitHost bool
386402
}
387403

388404
// User returns a [Userinfo] containing the provided username

0 commit comments

Comments
 (0)