I guess the package html/template is what you need. It allows you to turn the HTML code into a template file with placeholders. Your Go code can then fill the placeholders with data.
The whole process is too much to explain here, but here are some links to get you started:
The text/template package documentation is a reference of all the template functionality. (html/template works in the same way but has additional, HTML-related security features built in.)
It seems it is a very big work.
The go file is already doing lot of logic works to produce the data to the template.
Now additionally I have to maintain that file to implement the reusable ui.
Is this the only solution?
or
can I duplicate the ui? It seems easier to maintain instead of maintaining the ui in the go file…
What is the right approach? please advice.
I was using React for frontend. This is the first time I use go for front end. I used go for only rest api
It has very different comparing with React.
I do not know if I understand your question correct. But as @christophberger suggest html templates is also my choice to create “components” that you can reuse. Note that Go has no built in UI, so you have to use the HTML and CSS to create the UI.
First you create a “component”
{{define "component"}} // name the component accordingly
<p>This is a component</p>
{{end}}
And then you can reuse this component as many times you like in other templates.
<h1>Here is the base template</h1>
{{template "component"}} //this will call the html component
...
<h1>Here is another template</h1>
{{template "component"}} //this will call the same html component
...
It is normal to send data to the template {{.}} regardless of it i s a component or a base template. You can pass the data from the base template to the component by simply use the period.
{{template "component" . }}
If the HTML is good in the browser you have done it in the “right way”.
Go is not a framework. But using templates it behaves as a framework sort of. Not far from Angular IMO. Here is how one of my base templates looks like. Reusing components to make it more DRY…