Live data from Hacker News

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

news.ycombinator.com

91–96 of 96 posts

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

#91

You can use go’s builtin build features and embed the results into a http.FS. go generate npm build go build And declaring the resulting frontend build as an embed.FS using //go:embed dist/ var frontend *embed.FS And use that in a http.FS https://blog.logrocket.com/using-go-generate-reduce-boilerpl...

You can also use `go generate` to create all the .html routes/handlers at compile. Not that it would matter much in a small app.

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

#92
post #53

I've been using Go's html/template for the last couple of months and all I can say is that it's still very much a toy. It's fine for basic loops and conditionals but anything beyond that is extremely limited and having proper reusable components is a constant fight against limitations, specifically because of the lack of being able to pass multiple arguments to a template. I wish the Go team had gone with something m…

> because of the lack of being able to pass multiple arguments to a template

I'm not sure if this I understand the issue correctly, but one can pass multiple arguments, by wrapping them e.g. into a hash (https://go.dev/play/p/89gP42K8XRb):

  package main
  
  import (
      "log"
      "os"
      "text/template"
  )
  
  func main() {
      err := template.Must(
          template.New("").Parse("{{ .greeting }}, {{ .user.name }}!"),
      ).Execute(os.Stdout, map[string]any{
          "user": map[string]any{
              "name": "earthling",
          },
          "greeting": "hello",
      })
      if err != nil {
          log.Fatal(err)
      }
  }

In my experience, the module is far from a toy, but it's not always obvious how to use it properly, at first.

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

#94
post #78

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…

I’m really interested in your approach to building web apps in Go. I used to build toy projects with Go using just the standard libraries. What are the current frameworks or tools that are popular or recommended for building web applications with Go these days?

Go, Templ + Tailwindcss + AlpineJS
Post reply on HN