Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from ERB to Go Templates #2199

Open
alexandear opened this issue Dec 18, 2024 · 1 comment
Open

Migrate from ERB to Go Templates #2199

alexandear opened this issue Dec 18, 2024 · 1 comment

Comments

@alexandear
Copy link
Contributor

I propose to get rid of ERB in favor of native Go templates. Go has powerful generation tools, and it's better to use them.

Motivation

In a Go project, it is generally better to use Go tools for template generation to maintain consistency and leverage the native capabilities of the language. Using Go templates instead of ERB (Embedded Ruby) will:

  • Improve maintainability by using a single language for both code and templates.
  • Enhance readability for Go developers who may not be familiar with Ruby.
  • Simplify the build process by reducing dependencies on external tools.

Tasks

  1. Identify all ERB templates currently used in the project.
  2. Convert each ERB template to an equivalent Go template.
  3. Use Makefile (Taskfile, simple script) instead of Rakefile.

Example of converting one file

int_test.go.erb to int_test.go.tmpl

Current ERB Template

<% [2, 4, 8].each do |pg_byte_size| %>
<% pg_bit_size = pg_byte_size * 8 %>
func TestInt<%= pg_byte_size %>Codec(t *testing.T) {
    // ...
}
<% end %>

Equivalent Go Template:

{{ range $pg_byte_size := .ByteSizes }}
{{ $pg_bit_size := mul $pg_byte_size 8 }}
func TestInt{{ $pg_byte_size }}Codec(t *testing.T) {
    // ...
}
{{ end }}

Links

@jackc
Copy link
Owner

jackc commented Dec 21, 2024

While in theory I would prefer to use Go rather than another language and avoid the Ruby dependency, for templates I am not convinced. The erb templates are self-contained. But text/template would need some sort of setup to register the mul function and to pass in .ByteSizes.

Also, in my option, text/template syntax is rather awkward. e.g. pg_byte_size * 8 vs. mul $pg_byte_size 8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants