urlutil

package
v0.0.0-...-957a8c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2023 License: Unlicense Imports: 2 Imported by: 0

Documentation

Overview

Package urlutil contains types and utilities for dealing with URLs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type URL

type URL struct {
	url.URL
}

URL is a wrapper around url.URL that can marshal and unmarshal itself from text form more easily.

Example
package main

import (
	"encoding/json"
	"fmt"
	"net/url"

	"github.com/AdguardTeam/golibs/netutil/urlutil"
)

// check is an error-checking helper for examples.
func check(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	type jsonStruct struct {
		Stdlib *url.URL
		Util   *urlutil.URL
	}

	const rawURL = "https://host.example:1234/path?query=1#fragment"

	stdlibURL, err := url.Parse(rawURL)
	check(err)

	utilURL, err := urlutil.Parse(rawURL)
	check(err)

	v := &jsonStruct{
		Stdlib: stdlibURL,
		Util:   utilURL,
	}

	data, err := json.MarshalIndent(v, "", "  ")
	check(err)

	fmt.Printf("%s\n", data)

	v = &jsonStruct{}
	data = []byte(`{"Util":"` + rawURL + `"}`)
	err = json.Unmarshal(data, v)
	check(err)

	fmt.Printf("%q\n", v.Util)

}
Output:

{
  "Stdlib": {
    "Scheme": "https",
    "Opaque": "",
    "User": null,
    "Host": "host.example:1234",
    "Path": "/path",
    "RawPath": "",
    "OmitHost": false,
    "ForceQuery": false,
    "RawQuery": "query=1",
    "Fragment": "fragment",
    "RawFragment": ""
  },
  "Util": "https://host.example:1234/path?query=1#fragment"
}
"https://host.example:1234/path?query=1#fragment"

func Parse

func Parse(rawURL string) (u *URL, err error)

Parse is a wrapper around url.Parse that returns *URL.

func (*URL) MarshalText

func (u *URL) MarshalText() (b []byte, err error)

MarshalText implements the encoding.TextMarshaler interface for *URL.

func (*URL) UnmarshalText

func (u *URL) UnmarshalText(b []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface for *URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL