utils

package module
v0.0.0-...-d4d6daa Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 9 Imported by: 0

README

AtomicGo | utils

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/utils

utils

import "atomicgo.dev/utils"

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

Index

func AppendToFile

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.

func DownloadFile

func DownloadFile(url, path string) error

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

func Fetch

func Fetch(url string) (string, error)

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

func FileExists

func FileExists(path string) bool

FileExists returns true if the given file exists.

func PrettyJSON

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).

Example

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
}

func ReadFile

func ReadFile(path string) (string, error)

ReadFile reads the given file and returns its content.

func Ternary

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.

Example

package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

func main() {
	fmt.Println(utils.Ternary(true, "a", "b"))
	fmt.Println(utils.Ternary(false, "a", "b"))

}
Output
a
b

func ToInt

func ToInt[T string | constraints.Number](v 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)

Example

package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

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

func ToJSON

func ToJSON(v any) (string, error)

ToJSON converts the given value to a JSON string.

Example

package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	var person = Person{"John Doe", 42}

	json, _ := utils.ToJSON(person)
	fmt.Println(json)

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

func ToPrettyJSON

func ToPrettyJSON(v any, indent ...string) (string, error)
Example

package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

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
}

func ToString

func ToString(v any) string

ToString converts the given value to a string.

Example

package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	person := Person{"John Doe", 42}

	fmt.Println(utils.ToString(person))

}
Output
{John Doe 42}

func WriteFile

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  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToFile

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.

func DownloadFile

func DownloadFile(url, path string) error

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

func Fetch

func Fetch(url string) (string, error)

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

func FileExists

func FileExists(path string) bool

FileExists returns true if the given file exists.

func PrettyJSON

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).

Example
person := Person{Name: "John Doe", Age: 42}
json, _ := utils.ToJSON(person)
prettyJson, _ := utils.PrettyJSON(json)
fmt.Println(prettyJson)
Output:

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

func ReadFile

func ReadFile(path string) (string, error)

ReadFile reads the given file and returns its content.

func Ternary

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.

Example
package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

func main() {
	fmt.Println(utils.Ternary(true, "a", "b"))
	fmt.Println(utils.Ternary(false, "a", "b"))

}
Output:

a
b

func ToInt

func ToInt[T string | constraints.Number](v 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)

Example
package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

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

func ToJSON

func ToJSON(v any) (string, error)

ToJSON converts the given value to a JSON string.

Example
package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	var person = Person{"John Doe", 42}

	json, _ := utils.ToJSON(person)
	fmt.Println(json)

}
Output:

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

func ToPrettyJSON

func ToPrettyJSON(v any, indent ...string) (string, error)
Example
package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

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
}

func ToString

func ToString(v any) string

ToString converts the given value to a string.

Example
package main

import (
	"atomicgo.dev/utils"
	"fmt"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	person := Person{"John Doe", 42}

	fmt.Println(utils.ToString(person))

}
Output:

{John Doe 42}

func WriteFile

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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