Go templates VS GO API's

I’m new to Go…i’ve done the basics of it and now i have started learning making web applications in Go for my web application project…My question here is which way is should go…(once again i’m new to it so i dont know enough) templates in Go or making api’s in Go. Which is better way to go to make a web application in Go? And also what’s the diffrence between using templates and making api’s?

1 Like

Well, basically you have two kind of applications, normal ones where you navigate from page to page and single page applications (or SPA). Both applications can use templates and is recommended to use templates. If the SPA also use mostly API endpoints from backend is not an uncommon thing to have API calls in any applications. So i guess you can consider the both.

thanks for the response…so templates is the way to go for a web application in Go…but is using templates better than API…or its just better to go with templates for learning prospective?

Using templates is a good practice in any web applications because make your code more clear. But you can also use API calls in templates. So, i see no reason to go only in one direction because surely you will need both in your applications.

ok now i got it…thanks for the help:)

It really comes down to your application or what you’re looking to build / learn. As @geosoft1 mentioned there are basically two approaches:

1 - The SPA Approach
Building API endpoints is a pretty common methodology nowadays. Typically, you want to use this approach if you have more than one user interface a) iOS/Android b) Web Interface c) Console applications

Building an API in this scenario would allow your interfaces to interact with your backend API endpoints and share a ton of common code.

2 - The Server Side Approach
Traditionally, web applications have been server-side applications. A lot of the time you will hear this referred to as “MVC” (Model-View-Controller).

In this world, you have a web application that handles everything for your application (rendering a template, processing form submission, filtering table data, etc). You click a button on your web page and it submits to your server to do some processing. You click on a link and the server re-renders a template for the entire application with the information for that link.

Neither approach is bad or better than the other. Each has their pros and cons. More and more you will see developers leaning towards the API and SPA implementation, because it is the most popular. But the server-side approach is a very straight forward and easy way to get started and build an application that does something.

1 Like

So as from a begineer prospective i should learn the templates first and then move on to API’s or can i learn them in parallel?

Hi. Learn them in parallell if you can. They share a lot of familiarities one serve html and the other one json. The code is quite similar up to:

tmpl.Execute(w, “about.html”, data)

json.NewEncoder(w).Encode(data)

I am also a newbie, but have played with both templates and API. AFAIK they do different things. Templates serves your static website (at least mine). But the API serves the dynamic part (data from SQL servers etc)

Theoretically you can build a website with templates only. But the API makes it more dynamic on-the-fly.

I did learn the templates first and then add the API part afterwards.

FWIW

Moreover, for traditional (server side) applications also consider at least gorilla toolkit otherwise your life will be painful in dealing with sessions between pages :roll_eyes:

No. Templates vs APIs has nothing to do with your database but with how you render html.

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