Skip to content

Repository files navigation

AtomicGo



AtomicGo | utils

DownloadsLatest ReleaseTestsCoverageUnit test countLicense: MITGo report


Documentation | Contributing | Code of Conduct


go get atomicgo.dev/utils

utils

import"atomicgo.dev/utils"

Package utils is a collection of useful, quickly accessible utility functions.

Index

funcAppendToFile[Tstring| []byte](pathstring, contentT) error

AppendToFile appends the given content to the given file. Accepts a string or a byte slice.

funcDownloadFile(url, pathstring) error

DownloadFile downloads the given URL to the given path. If the file already exists, it will be overwritten.

func Fetch

funcFetch(urlstring) (string, error)

Fetch returns the body of a GET request to the given URL.

funcFileExists(pathstring) bool

FileExists returns true if the given file exists.

funcPrettyJSON(inputJSONstring, indent...string) (string, error)

PrettyJSON returns a pretty-printed JSON string. If indent is not provided, it defaults to " " (two spaces).

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
}

funcReadFile(pathstring) (string, error)

ReadFile reads the given file and returns its content.

func Ternary

funcTernary[Tany](conditionbool, a, bT) T

Ternary is a ternary operator. It returns a if the condition is true, otherwise it returns b.

package main
import (
"fmt""atomicgo.dev/utils"
)
funcmain() {
fmt.Println(utils.Ternary(true, "a", "b"))
fmt.Println(utils.Ternary(false, "a", "b"))
}

Output

a
b

func ToInt

funcToInt[Tstring| constraints.Number](valueT) 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)

package main
import (
"fmt""atomicgo.dev/utils"
)
funcmain() {
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

func ToJSON

funcToJSON(vany) (string, error)

ToJSON converts the given value to a JSON string.

package main
import (
"fmt""atomicgo.dev/utils"
)
typePersonstruct {
NamestringAgeint
}
funcmain() {
person:=Person{"John Doe", 42}
json, _:=utils.ToJSON(person)
fmt.Println(json)
}

Output

{"Name":"John Doe","Age":42}

funcToPrettyJSON(vany, indent...string) (string, error)

ToPrettyJSON converts the given value to an indented JSON string.

package main
import (
"fmt""atomicgo.dev/utils"
)
typePersonstruct {
NamestringAgeint
}
funcmain() {
person:=Person{Name: "John Doe", Age: 42}
prettyJSON, _:=utils.ToPrettyJSON(person)
fmt.Println(prettyJSON)
}

Output

{
"Name": "John Doe",
"Age": 42
}

funcToString(vany) string

ToString converts the given value to a string.

package main
import (
"fmt""atomicgo.dev/utils"
)
typePersonstruct {
NamestringAgeint
}
funcmain() {
person:=Person{"John Doe", 42}
fmt.Println(utils.ToString(person))
}

Output

{John Doe 42}

funcWriteFile[Tstring| []byte](pathstring, contentT) error

WriteFile writes the given content to the given file. Accepts a string or a byte slice.

Generated by gomarkdoc


AtomicGo.dev · with ❤️ by @MarvinJWendt | mjw.dev

About

✨ The most sophisticated utils package for Go

Topics

Resources

Code of conduct

Stars

2 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Generated from atomicgo/template