Live data from Hacker News

Show HN: Use Go's HTML/template to write React-like code

news.ycombinator.com

31–40 of 96 posts

Re: Show HN: Use Go's HTML/template to write React-like code

#31
post #9
post #3

Go's template system (both text/template and html/template) is pretty powerful. You can add custom functions and partial templates too. I use these concepts in https://docs.hofstadter.io to generate any code, not just frontend. Hugo uses the same system to do static site generation, which may be even closer to what you are showing. Helm templates is the epitome of the worst use (go/templating yaml files, I use CUE fo…

I love React too, but it pulls in a lot of dependencies and the dependencies need periodic maintenance. Sometimes you get the feeling that you're spending a significant chunk of your time not writing code but fighting with configs and dependency churn. Using Go's html/template definitely makes the UI feel dated because of re-rendering, but I rarely notice it in my side project. My next exploration is to use HTMX. I'm…

You can absolutely do it with HTMX. It'll feel SPA-smooth without all of the complexity. Some companies have rich and responsive UIs using plain HTML and an HTMX-like lib on top of it (like Hey.com). Pretty smooth and can be even reactive.

This is a reaction video (sorry) because I couldn't find the original, but in it you see plenty of things associated with SPAs being done in HTMX, from a company that went from React to HTMX: https://www.youtube.com/watch?v=wIzwyyHolRs

Add a few animations and transitions on top and I doubt anyone could tell the difference.

Re: Show HN: Use Go's HTML/template to write React-like code

#32
post #12

This is nice. Thanks for sharing. When I heard "react" I immediately thought SPA and Event handling in the browser. This is more close to ASP .NET ( organizing views on the server side). This is very useful but this is not for handling events on the client. Thanks again for sharing.

Yes, it's more about organizing your UI code/templates similar to a React codebase than running Go code in browser. I think apart from vDOM, a big contribution from React is how we organize code.

You are using “React-like” to mean something people don’t usually attribute to React, hence why there’s so much confusion in the comments. React did not invent component-driven code organization. I would say React is better known for reactive, declarative UI code (the reason for its name) and JSX.

Re: Show HN: Use Go's HTML/template to write React-like code

#33
post #27

A bit off-topic, and maybe it was just an example made without too much thought put into it, but I've noticed there seems to be a lot of people now who write button/links like this: My Button As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post. I think…

> As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post.

It definitely feels weird to have anything other than text nodes inside , didn't realize it's actually invalid HTML.

I used this approach mainly to have the button component behave as a link button and a form submit button. Here's the actual code from my project - https://github.com/sheshbabu/mouji/blob/master/commons/compo...

What are the drawbacks of this approach? I'm pretty sure I've used this before, can't recall any downsides.

Re: Show HN: Use Go's HTML/template to write React-like code

#34
post #24

My style for building web app. I was never convinced that doing everything on JS layer is a good thing (regardless the backend language). But you need to show more interactivity to convince the JS people. I personally like this style and add HTMX for partial updates.

Depends on what you want to do

Re: Show HN: Use Go's HTML/template to write React-like code

#35
post #27

A bit off-topic, and maybe it was just an example made without too much thought put into it, but I've noticed there seems to be a lot of people now who write button/links like this: My Button As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post. I think…

yeah it is invalid html, and probably done for the ui only. now people are just copying it. would totally break if the button anchor was in a form

Re: Show HN: Use Go's HTML/template to write React-like code

#36
post #27

A bit off-topic, and maybe it was just an example made without too much thought put into it, but I've noticed there seems to be a lot of people now who write button/links like this: My Button As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post. I think…

Once the major browser engines support it, it's de-facto legal.

This also explains why there are so few alternative browsers. Writing a new browser that supports everything that happens to work in the major browsers is ridiculously hard.

Re: Show HN: Use Go's HTML/template to write React-like code

#37
I remember the Astro web framework was written in Go before they moved to TypeScript. If there's a lot of JSX on the page and it needs to be escaped, what is the performance impact? Could you optimize the code to minimize slowdowns from templates?

Hugo, Jet, and other template engines are certainly interesting, but I chose to write in the Go template, which I later migrated to Astro with TypeScript. This was due to the challenges of managing two codebases for a medium-sized site as a solo dev.

Re: Show HN: Use Go's HTML/template to write React-like code

#38
post #36
post #27

A bit off-topic, and maybe it was just an example made without too much thought put into it, but I've noticed there seems to be a lot of people now who write button/links like this: My Button As far as I know that's technically invalid HTML. You can't have an as a child of a (or even the other way around.) I'm also ignoring the invalid type="submit" against the tag in the example button component in the post. I think…

Once the major browser engines support it, it's de-facto legal. This also explains why there are so few alternative browsers. Writing a new browser that supports everything that happens to work in the major browsers is ridiculously hard.

The big innovation of the html5 spec was to specify how to handle all the “invalid” html states; recovery from user error is now part of the spec instead of wholly implementation specific.

Re: Show HN: Use Go's HTML/template to write React-like code

#39

This is nice. Thanks for sharing. When I heard "react" I immediately thought SPA and Event handling in the browser. This is more close to ASP .NET ( organizing views on the server side). This is very useful but this is not for handling events on the client. Thanks again for sharing.

Yeah this is more like go-template-jsx than "react"

It’s not even JSX-like templates in Go because one of the biggest advantages of JSX is that for any logic you just use JavaScript instead of having to learn a template language. It’s extending HTML in JS rather than extending HTML to allow for scripting. I agree with the parent comment that this is more like the Go version of ASP.NET templates.

Re: Show HN: Use Go's HTML/template to write React-like code

#40
I've been doing something very similar in Rust recently using Askama or Maud for templates (components), optionally with Axum to include a server in the binary.

The author mentions wanting to colocate templates with logic, Maud allows much tighter/automatic integration in this regard.

This approach also synergizes almost perfectly with HTMX.

Post reply on HN