Skip to content

Commit

Permalink
added example and improved godoc (#2)
Browse files Browse the repository at this point in the history
* added example and improved godoc

* fixed linting
  • Loading branch information
MarvinJWendt authored Oct 31, 2023
1 parent a3d185e commit fe5e0b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
/*
F is the closest thing to template literals in Go.
F is a simple, fast and safe way to format strings in Go, with a familiar syntax.
It evaluates expressions inside `${}` and replaces them with their values.
The expressions support many operators, including ternary operator, and function calls.
See the syntax here: https://expr.medv.io/docs/Language-Definition
*/
package f
33 changes: 33 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
package f_test

import (
"fmt"

"atomicgo.dev/f"
)

type Person struct {
Name string
Age int
}

func Example_demo() {
// Format a string with a struct
john := Person{Name: "Bob", Age: 22}
fmt.Println(f.Format("${Name} is ${Age} years old", john))

// Format a string with a map
alice := map[string]any{"Name": "Alice", "Age": 27}
fmt.Println(f.Format("${Name} is ${Age} years old", alice))

// Evaluate an expression
fmt.Println(f.Format("John is 22 years old: ${Age == 22}", john))

// Ternary operator
fmt.Println(f.Format("John is 22 years old: ${Age == 22 ? 'yes' : 'no'}", john))

// Output:
// Bob is 22 years old
// Alice is 27 years old
// John is 22 years old: true
// John is 22 years old: yes
}
2 changes: 1 addition & 1 deletion f.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package f

// Format formats the template string.
func Format(template string, data ...any) string {
res, _ := FormatSafe(template, data)
res, _ := FormatSafe(template, data...)
return res
}

Expand Down

0 comments on commit fe5e0b6

Please sign in to comment.