Live data from Hacker News

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

news.ycombinator.com

61–70 of 96 posts

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

#61

This is a nice approach I've followed myself before. It feels like common sense web development, just writing things in a way which allows them to be reused. A button will likely be the same on any page of the site, so make a button component. I don't think this is "React-like" though. Making components isn't specific to React. We've been doing that since ASP and probably earlier. Your views still need to know what c…

I 100% know it's a preference/style kind of thing, and I know the React/Node isn't perfect and comes with it's downfalls, but I whenever I have to code in anything other, I just miss the visual/mental model aspect of having your tsx code basically identical to your actual html.

The mental load of going from this to

    func main() {
     component := Page{
      Children: []Component{
       Div{
        Children: []Component{
         Button{Name: "Test button"},
        },
       },
      },
     }
to this

  
    
       Test button
    
  `
or going from this:

  const main = () => (
    
      
         Test button
      
    
  )
To, well... case in point, exactly the same thing is just so nice.

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

#62
post #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…

> I used this approach mainly to have the button component behave as a link button and a form submit button.

Personally I think the "proper" way to do this would be use just for links, and for submitting forms, and then have the same CSS styles applied to both so that both 's and 's look the same visually if that's what you're going for.

I'm not particularly familiar with Go, but I'd either make the wrapping tag name itself a template variable (maybe an enum between "Anchor" and "Button"? again not familiar with Go) or have two components one for forms and one for links. If I've understood the syntax, something like this?

    
But given both 's and 's have lots of different attributes to manage its behaviour and state (name/value/type/disabled vs href/rel/target etc) I'd personally split them into two separate components, even if it ends up doubling up some of the logic and styles.

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

As others have mentioned, while it's technically invalid browsers will still happily accept it so there's no real issue with it, personally I just find it a bit quirky to read. There's no harm in leaving it just as it is :)

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

#63
post #18

Earlier quoted context omitted.

HTMX + React style server rendered components would solve 80% of the use cases for a modern SPA. Keep going! This is a breath of fresh air for many simpler applications.

I haven't tried HTMX, can it fetch and load html fragments into an existing dom?

That's pretty much all it does. It reacts to an event, fetches from an URL, swaps the result from that URL in some part of the DOM.

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

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

Yep, the HTML5 spec has an agreed on way to parse and interpret invalid HTML, so all modern browsers should handle it pretty consistently.

I was more interested in finding out where this style had come from really, because I've seen a lot of devs doing something similar and semantically it's just always struck me as a bit odd looking!

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

#65
post #9

Earlier quoted context omitted.

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…

There eventually has to be JavaScript somewhere, and once you have to use that for any dynamic frontend, might as well use a proper JS/TS framework. Not being a frontend person first, I have learned to choose whatever they are using these days. It will be hard to hire, onboard, and maintain anything that strays too far for the normal patterns they use. Next has skyrocketed in popularity for a reason. It makes the Rea…

"might as well"

Those are the most expensive 3 words in the english language!

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

#66

Interesting. I am actually doubling down on building web apps in Go using mostly standard library, powerful templates and sprinkle in with a library such as Alpine JS. Seeing some success so far and it makes me so much happier knowing that I don't have to deal with node_modules BS (well may be except tailwind for css). I actually want to test the limits of how far one can go without using a JS framework like React/Vu…

This is the direction I've moved to as well. I've used ROR and Clojure w/ heavyweight frontends in the past, but I eventually came around to the idea that I value Go w/ stdlib to avoid dependencies/framework churn, have painless deployments, and get extremely solid performance w/ minimal memory footprint out of the box without doing anything special.

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

#67
post #61

This is a nice approach I've followed myself before. It feels like common sense web development, just writing things in a way which allows them to be reused. A button will likely be the same on any page of the site, so make a button component. I don't think this is "React-like" though. Making components isn't specific to React. We've been doing that since ASP and probably earlier. Your views still need to know what c…

I 100% know it's a preference/style kind of thing, and I know the React/Node isn't perfect and comes with it's downfalls, but I whenever I have to code in anything other, I just miss the visual/mental model aspect of having your tsx code basically identical to your actual html. The mental load of going from this to func main() { component := Page{ Children: []Component{ Div{ Children: []Component{ Button{Name: "Test…

fair enough, but you're also downplaying/omitting the huge dependency cost of React, both server side and client side. so pick your poison.

you're basically saying "I want server side code without having to actually code anything". of course nothing is going to beat react, its an impossible ask

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

#68
post #63

Earlier quoted context omitted.

I haven't tried HTMX, can it fetch and load html fragments into an existing dom?

That's pretty much all it does. It reacts to an event, fetches from an URL, swaps the result from that URL in some part of the DOM.

Then does the server handling the requests needs to produce html fragments?

If I request a page, how does it know to only serve / fetch / render the main content vs the full page?

---

It seems like a lot of would be logic a real language has been moved to DSL and strings, i.e.

https://htmx.org/docs/#swap-options

`hx-on:htmx:config-request="event.detail.parameters.example = 'Hello Scripting!'"`

Not a fan of this fad of moving programming to embedded strings, also seen in lots of Yaml based systems

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

#69
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've given my Go templates a double suffix like index.go.html and index.go.json. Most tools will recognize these as HTML and JSON files so highlighting works as expected. Additionally in JetBrains IDEs you can assign the "GoTemplate" language to .go.html and .go.json files and it will also recognize the template/html and template/text syntax. So this gives you full syntax highlighting of both HTML/JSON and Go templates.

See https://github.com/PDOK/gokoala?tab=readme-ov-file#intellij-... for a script to set this up.

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

#70

Earlier quoted context omitted.

There eventually has to be JavaScript somewhere, and once you have to use that for any dynamic frontend, might as well use a proper JS/TS framework. Not being a frontend person first, I have learned to choose whatever they are using these days. It will be hard to hire, onboard, and maintain anything that strays too far for the normal patterns they use. Next has skyrocketed in popularity for a reason. It makes the Rea…

"might as well" Those are the most expensive 3 words in the english language!

It's very natural for humans to avoid choice perplexity and deviating from the norm when an unknown ROI or LOI is involved for alternatives. Sticking with the tried and true certainty has value and can reduce unforeseen or secondary "costs"
Post reply on HN