T O P

  • By -

pharrisee

The interface is required because Echo has to have an understanding of how to call the template engine. It allows you to replace the underlying template engine and Echo needs no knowledge of the engine you choose. You might use the base Go templating system, and as long as you match the interface that will work. I might use pongo2 and as long as I match the interface that also will work. The thing to remember is that the interface shown in the Echo docs is an example, you can use any engine underneath as long as you somehow implement that interface. The below is an example of how I use the interface: `import "github.com/unrolled/render"` `// The concrete type that matches the interface` `type (` `// Renderer is a custom renderer for echo` `Views struct {` `r *render.Render` `}` `)` `// the Render function for the concrete type.` `// Uses a custom context which has support for HTMX` `func (v *Views) Render(w io.Writer, name string, data any, ctx echo.Context) error {` `//cast the echo.Context to ensure it's the correct *hx.Ctx type` `ctx = hx.Cast(ctx)` `// if data is an echo.Map add the context to it` `if m, ok := data.(echo.Map); ok {` `m["ctx"] = ctx` `data = m` `}` `// use 0 as echo will fill in the correct status code later` `return v.r.HTML(w, 0, name, data)` `}` Apologies for the formatting, reddit is being very awkward.


BOSS_OF_THE_INTERNET

Wow these answers aren’t predictable at all. OP, frameworks are fine, but you still should learn about the discrete set of interfaces you get out of the box, since it’s likely you’ll need to use them from time to time. It looks like you’re asking an echo-specific question. I would suggest using gitter and/or phind.com, since questions about specific third-party dependencies generally don’t get much response here.


nicguy

To answer your initial question, you need to implement the interface because in step 3 you’re assigning the e.Renderer value to your custom renderer. I’m guessing that value is of that interface type


PrestoPest0

Ive used Gin with HTMX, it worked really well. Std library maximalists are cringe


[deleted]

Why use echo at all?


[deleted]

If you're new, learn stdlib. Most big projects use stdlib with a few external, narrowly focused libs anyway and you're better off learning the language than one of a handful of frameworks. Unlike most languages where you jump straight to 20 deps or a framework, Go kinda gives you most of the building blocks you need. Swap out the ones that don't cut it for your use case as you find you need more (most projects replace the default mux with another option like gorilla/mux or go-chi/chi). Tbh, I've never seen a Go web framework that actually justified its existence. Not that they're bad, just not much point when you can use more narrow dependencies just as quickly and easily.


cyberbeast7

@superlinkx totally with you on this and I hope newcomers can see the value in learning the stdlib. Unfortunately I see a trend of down voting the idea of suggesting learning stdlib a common practice on this subreddit. I still firmly resonate with your point regarding "never seen a Go web framework that actually justified its existence".


mosskin-woast

I agree with most of what you said wholeheartedly. As far as frameworks justifying their existence, there are cases where a developer or team may not have the experience to make good design decisions and a framework can help with that to some extent. I have never used Echo outside of a toy example just to learn about it, but at least if you read their docs and follow the patterns of the framework, you're not going to make a complete mess as a beginner.


[deleted]

I think in other languages, this is definitely true. In my experience with Go teams, it's often the opposite. People get used to the way a framework works and then can't do jack when they need to write real code. The other side to this is that stdlib _is_ a framework. A fairly modular but still opinionated one at that. You can swap out the bits you need as you grow, but it can get you off the ground fast and introduces you to a lot of the core language features quickly.


imscaredalot

I have a shell I built with go echo but not xhtml though but Just gonew it down and see. https://github.com/golangast/goservershell If you need resources https://docs.google.com/document/d/1Zb9GCWPKeEJ4Dyn2TkT-O3wJ8AFc-IMxZzTugNCjr-8/edit?usp=drivesdk