Compiled, executable documentation

Go URL examples

These examples come directly from checked-in Go source. Run them locally to exercise github.com/chengchuu/gurl; the website does not reimplement the package or send code to a remote execution service.

Run the basic command

The generator reads this listing from examples/basic/main.go. It parses a query string with GetQueryParam and checks complete and incomplete HTTP URLs with CheckValidHTTPURL.

go run ./examples/basic

Expected output

go
true
false

Open the basic example source on GitHub

// Command basic demonstrates query inspection with the public gurl package.
package main

import (
	"fmt"
	"log"

	"github.com/chengchuu/gurl"
)

func main() {
	value, err := gurl.GetQueryParam("https://example.com/search?q=go&lang=en", "q")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(value)
	fmt.Println(gurl.CheckValidHTTPURL("https://example.com"))
	fmt.Println(gurl.CheckValidHTTPURL("https:"))
}

Use an example test

The source-derived ExampleGetQueryParam function is compiled, and go test ./... verifies its output comment. Use this pattern for documentation that must stay synchronized with runtime behavior.

go test ./...

Open the example test source on GitHub

package gurl_test

import (
	"fmt"

	"github.com/chengchuu/gurl"
)

func ExampleGetQueryParam() {
	value, err := gurl.GetQueryParam("https://example.com/search?q=go&lang=en", "q")
	if err != nil {
		panic(err)
	}
	fmt.Println(value)
	// Output: go
}

func ExampleCheckValidHTTPURL() {
	fmt.Println(gurl.CheckValidHTTPURL("https://example.com"))
	fmt.Println(gurl.CheckValidHTTPURL("https:"))
	// Output:
	// true
	// false
}

Extend the examples safely

Modify query parameters

Use SetQueryParam or DelQueryParam and check the serialized URL returned by the package.

Work with hash routes

Use the hash-parameter helpers for fragments shaped like #route?key=value.