https://github.com/sheepla/go-urlbuilder
🔗 A Go module to easily build URL string with concise syntax
https://github.com/sheepla/go-urlbuilder
Last synced: about 1 year ago
JSON representation
🔗 A Go module to easily build URL string with concise syntax
- Host: GitHub
- URL: https://github.com/sheepla/go-urlbuilder
- Owner: sheepla
- License: mit
- Created: 2023-10-22T03:17:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-26T11:30:45.000Z (over 2 years ago)
- Last Synced: 2024-05-17T00:27:48.119Z (about 2 years ago)
- Language: Go
- Size: 18.6 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```go
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](https://github.com/sheepla)