https://github.com/atomicgo/f
🦄 The closest thing to template literals in Go. Parse expressions in strings - the simple way.
https://github.com/atomicgo/f
atomicgo evaluate expressions go golang golang-library hacktoberfest literal string template template-engine
Last synced: about 2 months ago
JSON representation
🦄 The closest thing to template literals in Go. Parse expressions in strings - the simple way.
- Host: GitHub
- URL: https://github.com/atomicgo/f
- Owner: atomicgo
- License: mit
- Created: 2023-10-30T16:50:04.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-08T17:04:17.000Z (11 months ago)
- Last Synced: 2024-08-08T19:59:45.030Z (11 months ago)
- Topics: atomicgo, evaluate, expressions, go, golang, golang-library, hacktoberfest, literal, string, template, template-engine
- Language: Go
- Homepage: https://atomicgo.dev
- Size: 39.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AtomicGo | f
---
Documentation
|
Contributing
|
Code of Conduct---
![]()
go get atomicgo.dev/f
# f
```go
import "atomicgo.dev/f"
```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
Example (Demo)
```go
package mainimport (
"fmt""atomicgo.dev/f"
)type Person struct {
Name string
Age int
}func main() {
// 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
```## Index
- [func Format\(template string, data ...any\) string](<#Format>)
- [func FormatSafe\(template string, data ...any\) \(string, error\)](<#FormatSafe>)
- [type Parsed](<#Parsed>)
- [func Parse\(template string\) Parsed](<#Parse>)
- [func \(parsed Parsed\) Eval\(data any\) \(string, error\)](<#Parsed.Eval>)
- [func \(parsed Parsed\) String\(\) string](<#Parsed.String>)
- [type Part](<#Part>)```go
func Format(template string, data ...any) string
```Format formats the template string.
```go
func FormatSafe(template string, data ...any) (string, error)
```FormatSafe formats the template string and returns an additional, optional error, if something goes wrong.
Parsed contains a parsed template string, ready to be evaluated.
```go
type Parsed struct {
Template string
Parts []Part
}
``````go
func Parse(template string) Parsed
```Parse parses a template string into a Parsed struct.
```go
func (parsed Parsed) Eval(data any) (string, error)
```Eval evaluated expressions in the parsed template string.
### func \(Parsed\) [String]()```go
func (parsed Parsed) String() string
```String returns the parsed template parts as a single string.
Part is a single part of a template string. Can either be a raw string, or an expression.
```go
type Part struct {
Value string
Parsed bool
}
```Generated by [gomarkdoc]()
---
> [AtomicGo.dev](https://atomicgo.dev) ·
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)