If statements in template

Say I need to loop through an array and make some checking in it check cannot be done in the normal Go template, How can I make a checker like this
{{ if 1=1}} It's true {{else}} It's false{{end}}
Doing this directly will give me an error, what should I do?
Wow, just realized that I can’t even do any simple math operators like:

{{3 + 1}}

If there is no way arround that or if I’m missing something then How am I supposed to build my app?!

This is not related to the question but... Should i use .html extension or .tmpl or .gotmpl?

You may need this side-by-side as a start: https://golang.org/pkg/text/template/. Template functions can be slightly different. For some (e.g. strings processing), you may need to port the strings functions before using the template file.

compare equal is:

{{ if eq 1 1 }}

Not a rule but the practice would be: .html.tmpl or .html.template. The first denotes the output extension while the second denotes it is a template.

If confused, stick to .tmpl as standard HTML does not interpret go template codes.


Is there any cheat sheet for those things or documentation?
Also, I’m using html/template, is there any difference between it and text/template? if yes which one should I use
Thank you so much for helping

Stick to html/template for HTML rendering. It handles a number of HTML security related matters like escaping and etc beneath it. For more info: https://golang.org/pkg/html/template/.

The documentation is a good starter.

If you want to get familiarity and practical experiences, you can try hackathon some Go Hugo static sites + theme from scratch. NOTE: build, not use.

The reason I recommend Go Hugo is you don’t need to write .go codes so you can focus on CSS, JS, and HTML with a lot of Go template functions. :joy: Good to relax if you’re overloaded.


I just tried to build an app, after spending about 3 hours to build the authentication system and debugging weird useless memory pointer addresses in the terminal I was finally able to get it to work. Or not, I can’t get the instance of the user! When I try to pass a user instance I keep getting an error; I don’t think I’m doing anything wrong here and if I were the log of errors is not helpful at all
which makes me start to regret moving to Golang. Remembers me of an answer on Quora (No one will notice the difference between 0.1 and 0.000001 loading speed)

Will a framework change all of that and make me able to code much faster without affecting the performance speed? Or should I stick to the builtin go libraries -with some packages as gorilla sessions or httprouter- and try to master it by building as

many pojects as possible?
Appreciate your help

I think I should :sweat_smile:

My experience of frameworks is that you are forced to think as the framework thinks (like JQuery vs Vanilla). You get the structure and the magic. Sometimes good and sometimes bad. If you find that Gin or Gorilla thinks like you, go for it. I have decided to avoid frameworks as long as possible. But I might change my mind in the future.

1 Like

This is experience question. As of this stage (of learning), accuracy and consistency is far more important than speed.

My advice: keep practicing and let the experience flows into you. You will go faster with stability for each development iteration.

No. Sounds like you’re grasping computer science (the theory side of working) vs. the programming implementation. Framework can only do so much by doing some magics as @Sibert mentioned. It can be harmful if you don’t grasp the actual com science concept. Some examples:

  1. did you implement constant time password comparison and understand why to do it?
  2. did you only use any of the slow algorithm for password hasing (bcrypt, scrypt, or Argon2)?

In situations where you want collective supports, then code library or modules would make a lot of sense (e.g. using AuthBoss).

The good news is that you can build and debug so programming is not a main problem.

Can’t comment. The only way is to execute some mini hackathon on your own (e.g. build 2 small url shorteners app where framework vs. library) and collect development process metrics. Your observations should give you some kind of pointers on when to use X framework and when should not that best suited with you. :joy:

E.g. At one time, I got fed up with working on a very large framework integrating with another large framework due to too many magics and not able to firmly control things. However, multiple small, manageable, and unit-tested modules are my current best working model, which is one of the reason I chosen Go.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.