Live data from Hacker News

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

news.ycombinator.com

1–10 of 96 posts

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

#1
Hi all,

I'm currently working on my hobby project, and one of the fun constraints I put in was to build using as less dependencies as possible.

I chose Go as it has a really good standard library, and all went well for building backend. But for frontend, I was wondering whether I should break the constraint and go for React. I tried options like Web Components, but I really didn't like the ergonomics and I didn't want to use jQuery either.

Out of curiosity, I was exploring Go's html/template package to see if I can write UIs in a React-like manner. I found most of the online docs using the "slots" like approach which I found unintuitive.

But after trial and error, I found an approach that's very close to React and without using any 3rd party packages like templ.

I'd like to share this with the community, not sure if it's a common approach - https://www.sheshbabu.com/posts/react-like-composition-using...

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

#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 for this now).

For frontend, as in your example, this has the older feel for UI, where each navigation requires a round trip to the server and a full-page response to the client. This is part of the reason people have moved to React and full-stack TS. I love Go, but I'm definitely a big fan of writing webapps in Next at this point. React Server Components are a really nice DX.

I now generate much of the React / Next code at develop time, rather than runtime. Hof's original goal was to make adding a field to a type a one-line change in a source-of-truth file, and then generate all the changes through a full-stack app by running `hof gen`.

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

#4
[off-topic comment but somewhat relevant for anyone choosing a lang/ecosystem]

I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete. We're using Go html/templates on a new project (within a team that does a lot of React frontend & some Python) so this kind of project seems like it would really suit, but a massive bugbear has been the very poor IDE-level syntax highlighting support for Go html/templates. Can anyone recommend a good syntax highlighter for Go html/template in VSCode for example? None of the main/official/popular addons seem to get this right.

Seems a pretty unexpected oversight from a language from Google.

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

#6
post #4

[off-topic comment but somewhat relevant for anyone choosing a lang/ecosystem] I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete. We're using Go html/templates on a new project (within a team that does a lot of React frontend & some Python) so this kind of project seems like it would really suit, but a massive bugbear has been the very poor IDE-level syntax highligh…

> I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete.

Sorry, it might be something wrong with my blog setup. I use hexo, might need to play around with the config.

I use this VSCode extension for coding and it works fairly well: https://marketplace.visualstudio.com/items?itemName=jinlimin...

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

#7
post #5
post #2

Here's the link: https://www.sheshbabu.com/posts/react-like-composition-using... Didn't realize HN doesn't convert link text to clickable links

Huh I was able to click on it fine, maybe it wasnt clickable initially?

Ah yes, maybe my mistake!

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

#8
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.

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

#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 planning to gradually add dependencies like HTMX to see how far I can go to reach React's smoothness.

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

#10
post #4

[off-topic comment but somewhat relevant for anyone choosing a lang/ecosystem] I noticed that the syntax highlighting for the Go html/template examples in the article is incomplete. We're using Go html/templates on a new project (within a team that does a lot of React frontend & some Python) so this kind of project seems like it would really suit, but a massive bugbear has been the very poor IDE-level syntax highligh…

there are also projects that let you use HTML directly in Go like: https://www.gomponents.com/ (I'm currently using it in a side project and liking it so far, but I also just started)
Post reply on HN