https://github.com/atomicgo/utils
✨ The most sophisticated utils package for Go
https://github.com/atomicgo/utils
atomicgo go golang golang-library utils
Last synced: about 2 months ago
JSON representation
✨ The most sophisticated utils package for Go
- Host: GitHub
- URL: https://github.com/atomicgo/utils
- Owner: atomicgo
- License: mit
- Created: 2023-09-08T22:56:33.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-08T17:04:43.000Z (11 months ago)
- Last Synced: 2024-11-09T08:43:33.037Z (7 months ago)
- Topics: atomicgo, go, golang, golang-library, utils
- Language: Go
- Homepage: https://atomicgo.dev
- Size: 45.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AtomicGo | utils
---
Documentation
|
Contributing
|
Code of Conduct---
![]()
go get atomicgo.dev/utils
# utils
```go
import "atomicgo.dev/utils"
```Package utils is a collection of useful, quickly accessible utility functions.
## Index
- [func AppendToFile\[T string | \[\]byte\]\(path string, content T\) error](<#AppendToFile>)
- [func DownloadFile\(url, path string\) error](<#DownloadFile>)
- [func Fetch\(url string\) \(string, error\)](<#Fetch>)
- [func FileExists\(path string\) bool](<#FileExists>)
- [func PrettyJSON\(inputJSON string, indent ...string\) \(string, error\)](<#PrettyJSON>)
- [func ReadFile\(path string\) \(string, error\)](<#ReadFile>)
- [func Ternary\[T any\]\(condition bool, a, b T\) T](<#Ternary>)
- [func ToInt\[T string | constraints.Number\]\(value T\) int](<#ToInt>)
- [func ToJSON\(v any\) \(string, error\)](<#ToJSON>)
- [func ToPrettyJSON\(v any, indent ...string\) \(string, error\)](<#ToPrettyJSON>)
- [func ToString\(v any\) string](<#ToString>)
- [func WriteFile\[T string | \[\]byte\]\(path string, content T\) error](<#WriteFile>)```go
func AppendToFile[T string | []byte](path string, content T) error
```AppendToFile appends the given content to the given file. Accepts a string or a byte slice.
```go
func DownloadFile(url, path string) error
```DownloadFile downloads the given URL to the given path. If the file already exists, it will be overwritten.
```go
func Fetch(url string) (string, error)
```Fetch returns the body of a GET request to the given URL.
```go
func FileExists(path string) bool
```FileExists returns true if the given file exists.
```go
func PrettyJSON(inputJSON string, indent ...string) (string, error)
```PrettyJSON returns a pretty\-printed JSON string. If indent is not provided, it defaults to " " \(two spaces\).
```go
person := Person{Name: "John Doe", Age: 42}
json, _ := utils.ToJSON(person)
prettyJSON, _ := utils.PrettyJSON(json)
fmt.Println(prettyJSON)// Output:
// {
// "Name": "John Doe",
// "Age": 42
// }
```#### Output
```
{
"Name": "John Doe",
"Age": 42
}
``````go
func ReadFile(path string) (string, error)
```ReadFile reads the given file and returns its content.
```go
func Ternary[T any](condition bool, a, b T) T
```Ternary is a ternary operator. It returns a if the condition is true, otherwise it returns b.
```go
package mainimport (
"fmt""atomicgo.dev/utils"
)func main() {
fmt.Println(utils.Ternary(true, "a", "b"))
fmt.Println(utils.Ternary(false, "a", "b"))}
```#### Output
```
a
b
``````go
func ToInt[T string | constraints.Number](value T) int
```ToInt converts the given value to an int. If the value is a float, it will be rounded to the nearest integer. \(Rounds up if the decimal is 0.5 or higher\)
```go
package mainimport (
"fmt""atomicgo.dev/utils"
)func main() {
fmt.Println(utils.ToInt(1337))
fmt.Println(utils.ToInt(1337.4))
fmt.Println(utils.ToInt(1337.5))
fmt.Println(utils.ToInt(1337.6))
fmt.Println(utils.ToInt("1337"))
fmt.Println(utils.ToInt("1337.4"))
fmt.Println(utils.ToInt("1337.5"))
fmt.Println(utils.ToInt("1337.6"))}
```#### Output
```
1337
1337
1338
1338
1337
1337
1338
1338
``````go
func ToJSON(v any) (string, error)
```ToJSON converts the given value to a JSON string.
```go
package mainimport (
"fmt""atomicgo.dev/utils"
)type Person struct {
Name string
Age int
}func main() {
person := Person{"John Doe", 42}json, _ := utils.ToJSON(person)
fmt.Println(json)}
```#### Output
```
{"Name":"John Doe","Age":42}
``````go
func ToPrettyJSON(v any, indent ...string) (string, error)
``````go
package mainimport (
"fmt""atomicgo.dev/utils"
)type Person struct {
Name string
Age int
}func main() {
person := Person{Name: "John Doe", Age: 42}
prettyJSON, _ := utils.ToPrettyJSON(person)
fmt.Println(prettyJSON)}
```#### Output
```
{
"Name": "John Doe",
"Age": 42
}
``````go
func ToString(v any) string
```ToString converts the given value to a string.
```go
package mainimport (
"fmt""atomicgo.dev/utils"
)type Person struct {
Name string
Age int
}func main() {
person := Person{"John Doe", 42}fmt.Println(utils.ToString(person))
}
```#### Output
```
{John Doe 42}
``````go
func WriteFile[T string | []byte](path string, content T) error
```WriteFile writes the given content to the given file. Accepts a string or a byte slice.
Generated by [gomarkdoc]()
---
> [AtomicGo.dev](https://atomicgo.dev) ·
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)