url

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0

README

URL Building Utilities

The url package provides utilities for building and manipulating URLs in Go applications using dependency injection.

Features

  • URLBuilder: Configurable URL builder with dependency injection
  • RootURL: Get the configured root URL
  • BuildURL: Create full URLs with paths and query parameters
  • BuildQuery: Convert parameter maps to query strings
  • HttpBuildQuery: Convert url.Values to query strings

Installation

import "github.com/dracory/base/url"

Usage

// Create a URL builder with your application's root URL
builder := url.NewURLBuilder("https://example.com")

// Build URLs
fullURL := builder.BuildURL("api/users", nil)
// Returns: "https://example.com/api/users"

// With query parameters
params := map[string]string{
    "page": "1",
    "limit": "10",
}
fullURL = builder.BuildURL("api/users", params)
// Returns: "https://example.com/api/users?page=1&limit=10"
Using Default Functions

For simple use cases, you can set a default URL and use the convenience functions:

// Set the default URL (typically done during application initialization)
url.SetDefaultURL("https://example.com")

// Use default functions
root := url.RootURL()
// Returns: "https://example.com"

fullURL := url.BuildURL("api/users", map[string]string{"active": "true"})
// Returns: "https://example.com/api/users?active=true"
Dependency Injection Pattern

The recommended approach is to inject the URLBuilder into your services:

type UserService struct {
    urlBuilder *url.URLBuilder
}

func NewUserService(urlBuilder *url.URLBuilder) *UserService {
    return &UserService{urlBuilder: urlBuilder}
}

func (s *UserService) GetUserURL(id string) string {
    return s.urlBuilder.BuildURL("users/"+id, nil)
}

// In your main.go
func main() {
    appURL := os.Getenv("APP_URL")
    urlBuilder := url.NewURLBuilder(appURL)
    
    userService := NewUserService(urlBuilder)
    // ...
}

Functions

NewURLBuilder(rootURL string) *URLBuilder

Creates a new URLBuilder with the given root URL.

Parameters:

  • rootURL: The base URL for all URLs built by this builder

Returns:

  • *URLBuilder: A new URLBuilder instance
URLBuilder Methods
RootURL() string

Returns the configured root URL.

BuildURL(path string, params map[string]string) string

Builds a complete URL by combining the root URL with a path and optional query parameters.

Parameters:

  • path: The path to append to the root URL
  • params: Optional map of query parameters

Returns:

  • string: The complete URL (empty string if root URL is not set)
BuildQuery(queryData map[string]string) string

Converts a map of string parameters to a URL query string.

Parameters:

  • queryData: Map of parameter key-value pairs

Returns:

  • string: Query string (including "?" if parameters exist)
HttpBuildQuery(queryData url.Values) string

Converts url.Values to a URL-encoded query string.

Parameters:

  • queryData: url.Values to encode

Returns:

  • string: URL-encoded query string
Default Functions
SetDefaultURL(rootURL string)

Sets the default URL builder's root URL for convenience functions.

RootURL() string

Returns the default root URL.

BuildURL(path string, params map[string]string) string

Builds a URL using the default builder.

BuildQuery(queryData map[string]string) string

Builds a query string using the default builder.

HttpBuildQuery(queryData url.Values) string

Builds a query string using the default builder.

Examples

// Dependency injection approach
func SetupServices(appURL string) {
    urlBuilder := url.NewURLBuilder(appURL)
    
    // Inject into services
    userService := NewUserService(urlBuilder)
    apiService := NewAPIService(urlBuilder)
    
    // Services can now build URLs without environment dependencies
    userURL := userService.GetUserURL("123")
    // "https://myapp.com/users/123"
}

// Default function approach
func InitializeApp() {
    appURL := os.Getenv("APP_URL")
    url.SetDefaultURL(appURL)
    
    // Throughout the application
    apiURL := url.BuildURL("api/v1/users", map[string]string{"active": "true"})
    // "https://myapp.com/api/v1/users?active=true"
}

Testing

The package includes comprehensive tests:

go test ./url

Dependencies

  • Go 1.16+
  • No external dependencies
  • No environment variable dependencies (when using URLBuilder)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildQuery

func BuildQuery(queryData map[string]string) string

BuildQuery creates a query string using default builder

func BuildURL

func BuildURL(path string, params map[string]string) string

BuildURL returns the full URL for a given path using default builder

func HttpBuildQuery

func HttpBuildQuery(queryData url.Values) string

HttpBuildQuery converts url.Values to a query string using default builder

func RootURL

func RootURL() string

RootURL returns the default root URL

func SetDefaultURL

func SetDefaultURL(rootURL string)

SetDefaultURL sets the default URL builder's root URL

Types

type URLBuilder

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

URLBuilder provides URL building functionality with configurable root URL

func NewURLBuilder

func NewURLBuilder(rootURL string) *URLBuilder

NewURLBuilder creates a new URLBuilder with the given root URL

func (*URLBuilder) BuildQuery

func (ub *URLBuilder) BuildQuery(queryData map[string]string) string

BuildQuery creates a query string from a map of parameters

func (*URLBuilder) BuildURL

func (ub *URLBuilder) BuildURL(path string, params map[string]string) string

BuildURL returns the full URL for a given path with optional query parameters

func (*URLBuilder) HttpBuildQuery

func (ub *URLBuilder) HttpBuildQuery(queryData url.Values) string

HttpBuildQuery converts url.Values to a query string

func (*URLBuilder) RootURL

func (ub *URLBuilder) RootURL() string

RootURL returns the configured root URL

Jump to

Keyboard shortcuts

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