f

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

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 4 Imported by: 0

README

AtomicGo | f

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/f

f

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)

package main

import (
	"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

func Format(template string, data ...any) string

Format formats the template string.

func FormatSafe

func FormatSafe(template string, data ...any) (string, error)

FormatSafe formats the template string and returns an additional, optional error, if something goes wrong.

type Parsed

Parsed contains a parsed template string, ready to be evaluated.

type Parsed struct {
    Template string
    Parts    []Part
}

func Parse
func Parse(template string) Parsed

Parse parses a template string into a Parsed struct.

func (Parsed) Eval
func (parsed Parsed) Eval(data any) (string, error)

Eval evaluated expressions in the parsed template string.

func (Parsed) String
func (parsed Parsed) String() string

String returns the parsed template parts as a single string.

type Part

Part is a single part of a template string. Can either be a raw string, or an expression.

type Part struct {
    Value  string
    Parsed bool
}

Generated by gomarkdoc


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

Documentation

Overview

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)
package main

import (
	"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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(template string, data ...any) string

Format formats the template string.

func FormatSafe

func FormatSafe(template string, data ...any) (string, error)

FormatSafe formats the template string and returns an additional, optional error, if something goes wrong.

Types

type Parsed

type Parsed struct {
	Template string
	Parts    []Part
}

Parsed contains a parsed template string, ready to be evaluated.

func Parse

func Parse(template string) Parsed

Parse parses a template string into a Parsed struct.

func (Parsed) Eval

func (parsed Parsed) Eval(data any) (string, error)

Eval evaluated expressions in the parsed template string.

func (Parsed) String

func (parsed Parsed) String() string

String returns the parsed template parts as a single string.

type Part

type Part struct {
	Value  string
	Parsed bool
}

Part is a single part of a template string. Can either be a raw string, or an expression.

Jump to

Keyboard shortcuts

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