funcs

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b any) (any, error)

Add adds two numbers.

func Ceil

func Ceil(x any) (float64, error)

Ceil returns the least integer value greater than or equal to x.

func Dict

func Dict(values ...any) (map[string]any, error)

func Div

func Div(a, b any) (any, error)

Div divides two numbers.

func Floor

func Floor(x any) (float64, error)

Floor returns the greatest integer value less than or equal to x.

func GetUtilsFuncMap

func GetUtilsFuncMap() map[string]any
Example
content := `
# {{.Title}}

## Dict
{{- $MyDict := dict "Name" "Carson" "ID" "001" }}
Hi {{ $MyDict.Name -}}. Data {{ (dict "A" "65" "B" "66").B }} {{ index (dict "A" "65" "C" "67") "C" }}
// Hi Carson. Data 66 67

## Slice
{{- $Numbers := list 1 3 5 -}}
{{- $Items := list "aa" "bb" "cc" }}
{{ index $Numbers 2 }} {{ index $Items 1 }} {{ index (list "a" "b" "c") 2 }}
// 5 bb c

{{ index (split "2022/05/23" "/") 0 }}
// 2022

{{ replace "Hi xxx" "xxx" "Carson" -1 }}
// Hi Carson

{{ replace "Hi XXX XXX" "X" "0" 2 }}
// Hi 00X XXX

## Math
可以善用:
replace:
\{\{.*\}\} = (.*)
$1 = $1
來把左側取代為右側內容
{{add 3 5}} = 8
{{add 3 0.5}} = 3.5
{{sub 1.5 -0.7}} = 2.2
{{mul 3 2}} = 6
{{mul 1.5 0.7}} = 1.0499999999999998
{{div 3 2}} = 1.5
{{mul 0 -0.7}} = -0
{{mul 0 0.7}} = 0
{{ceil 0}} = 0
{{ceil 0.1}} = 1
{{ceil -1.1}} = -1
{{floor 0.1}} = 0
{{floor -0.1}} = -1
{{ln 1}} = 0
{{ln 2}} = 0.6931471805599453
{{log 1 10}} = 0
{{log 2 10}} = 0.30102999566398114
{{sqrt 0}} = 0
{{sqrt 4}} = 2
{{mod 9 5}} = 4
{{mod -5 2}} = -1
{{mod 2.8 2}} = 0
{{modBool 9 5}} = false
{{modBool 0 5}} = true
{{modBool 2.8 2}} = true
{{pow 2 3}} = 8
{{pow 0 2}} = 0
{{pow 1.2 0}} = 1
{{round -1.4}} = -1
{{round 1.5000}} = 2
`

t := template.Must(new(template.Template).
	Funcs(GetUtilsFuncMap()).
	Parse(content))

buffer := bytes.NewBuffer(make([]byte, 0))
_ = t.Execute(buffer, map[string]string{"Title": "Example"})

fmt.Println(buffer)
Output:


# Example

## Dict
Hi Carson. Data 66 67
// Hi Carson. Data 66 67

## Slice
5 bb c
// 5 bb c

2022
// 2022

Hi Carson
// Hi Carson

Hi 00X XXX
// Hi 00X XXX

## Math
可以善用:
replace:
\{\{.*\}\} = (.*)
$1 = $1
來把左側取代為右側內容
8 = 8
3.5 = 3.5
2.2 = 2.2
6 = 6
1.0499999999999998 = 1.0499999999999998
1.5 = 1.5
-0 = -0
0 = 0
0 = 0
1 = 1
-1 = -1
0 = 0
-1 = -1
0 = 0
0.6931471805599453 = 0.6931471805599453
0 = 0
0.30102999566398114 = 0.30102999566398114
0 = 0
2 = 2
4 = 4
-1 = -1
0 = 0
false = false
true = true
true = true
8 = 8
0 = 0
1 = 1
-1 = -1
2 = 2

func List

func List(args ...any) any

func Ln

func Ln(a any) (float64, error)

Ln (Nature Log: base e) returns the natural logarithm of a number.

func Log

func Log(a any, b any) (float64, error)

Log to the base b of a

func Mod

func Mod(a, b any) (int64, error)

Mod returns a % b.

func ModBool

func ModBool(a, b any) (bool, error)

ModBool returns the boolean of a % b. If a % b == 0, return true.

func Mul

func Mul(a, b any) (any, error)

Mul multiplies two numbers.

func Pow

func Pow(a, b any) (float64, error)

Pow returns a raised to the power of b.

func Replace

func Replace(target, old, new any, n int) (string, error)

func Round

func Round(x any) (float64, error)

Round returns the nearest integer, rounding half away from zero.

func Split

func Split(obj any, delimiter string) ([]string, error)

func Sqrt

func Sqrt(a any) (float64, error)

func Sub

func Sub(a, b any) (any, error)

Sub subtracts two numbers.

Types

This section is empty.

Directories

Path Synopsis
Package compare provides template functions for comparing values.
Package compare provides template functions for comparing values.

Jump to

Keyboard shortcuts

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