gotpl

Crates.iogotpl
lib.rsgotpl
version0.2.6
created_at2025-09-20 03:24:08.355725+00
updated_at2025-10-01 13:37:28.073761+00
descriptionA Rust library providing full Go template (text/template and html/template) support via FFI.
homepagehttps://github.com/moyanj/gotpl
repositoryhttps://github.com/moyanj/gotpl
max_upload_size
id1847365
size34,181
MoYan (moyanj)

documentation

README

gotpl

Crates.io Docs.rs License: MIT

A Rust library for using Go's powerful text/template and html/template engines via FFI. Get the safety of Rust with the mature templating power of Go.

✨ Features

  • Full Go Template Support: Complete support for Go's text/template and html/template syntax.
  • HTML Safety: Automatic HTML escaping for XSS prevention via html/template.
  • Flexible Data: Render templates with any serde::Serialize-able Rust data.
  • Idiomatic Errors: Go template errors are converted into Rust Result types.
  • Memory Safe: Memory allocated by Go is safely managed and freed from Rust to prevent leaks.
  • Lightweight: Depends only on serde for data serialization.

🚀 Quick Start

Installation

Add to your Cargo.toml:

[dependencies]
gotpl = "0.2.3" # Use the latest version
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Example

use gotpl::TemplateRenderer;
use serde_json::json;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let template = "Hello, {{.name}}! You have {{len .items}} items.";
    let data = json!({
        "name": "MoYan",
        "items": ["book", "pen"]
    });

    let output = TemplateRenderer::new(template, &data).render()?;

    println!("{}", output);

    Ok(())
}

🌐 Go Template Syntax

This library supports the complete Go template syntax. For details, see the official docs:

🛠️ Build Process

gotpl uses a build.rs script to compile the Go source into a static library and generate Rust FFI bindings with bindgen.

Requirements:

  • Go Compiler (version 1.18+)
  • Rust Toolchain (Cargo)

The build is fully automated when you run cargo build.

⚠️ Caveats

  • Performance: FFI calls have overhead. Profile accordingly for high-performance use cases.
  • Binary Size: Includes the Go runtime, which will increase your final binary size.
  • Memory: All memory is managed safely across the FFI boundary.

🤝 Contributing

Contributions are welcome via Pull Requests or Issues.

📜 License

Licensed under the MIT License. See LICENSE for details.

Commit count: 8

cargo fmt