urlbuilder

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 2 Imported by: 1

README

🔗 go-urlbuilder

go-urlbuilder is a Go module based on net/url standard module, aimed at safely constructing URL strings with a concise syntax.

Why?

It is a good idea to use the net/url standard module when safely constructing URL strings. However, if you use net/url as is, you often need to prepare temporary variables, and have to write non-declaretive code which I felt was a bit cumbersome when building complex URLs over and over again.

This module was created as a concise and easy way to construct URL strings based on net/url.

Usage

package main

import (
	"fmt"
	"net/url"

	"github.com/sheepla/go-urlbuilder"
)

var sourceURL = "https://localhost:8080/path/to/resource#helloworld?key1=value1&key2=value2"

func main() {
	u := urlbuilder.MustParse(sourceURL)

	u.SetScheme("http").
		SetHost("example.com:12345").
		SetFragment("anotherFragment").
		EditPath(func(elements []string) []string {
			return append(elements, "Go言語")
		}).
		EditQuery(func(q url.Values) url.Values {
			q.Set("key1", "key1-edited")
			q.Del("key2")
			q.Add("key3", "value3")

			return q
		})

	// => http://example.com:12345/path/to/resource/Go%25E8%25A8%2580%25E8%25AA%259E?key1=key1-edited&key3=value3#anotherFragment
	fmt.Println(u.MustString())
}

License

MIT

Author

sheepla

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type URL

type URL struct {
	// contains filtered or unexported fields
}

func MustParse

func MustParse(s string) *URL

func Parse

func Parse(s string) (*URL, error)

func (*URL) AddQuery

func (u *URL) AddQuery(key, value string) *URL

func (*URL) AppendPath

func (u *URL) AppendPath(elements ...string) *URL

func (*URL) EditPath

func (u *URL) EditPath(editFunc func([]string) []string) *URL

func (*URL) EditQuery

func (u *URL) EditQuery(editFunc func(url.Values) url.Values) *URL

func (*URL) MustString

func (u *URL) MustString() string

func (*URL) RemoveQuery

func (u *URL) RemoveQuery(key string) *URL

func (*URL) SetFragment

func (u *URL) SetFragment(fragment string) *URL

func (*URL) SetHost

func (u *URL) SetHost(host string) *URL

func (*URL) SetPath

func (u *URL) SetPath(base string, elements ...string) *URL

func (*URL) SetQuery

func (u *URL) SetQuery(key, value string) *URL

func (*URL) SetScheme

func (u *URL) SetScheme(scheme string) *URL

func (*URL) SetUser

func (u *URL) SetUser(userName string) *URL

func (*URL) SetUserWithPassword

func (u *URL) SetUserWithPassword(userName, password string) *URL

func (*URL) String

func (u *URL) String() (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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