Golang HTML builder

Tabvn
1 min readAug 14, 2020

--

create simple html builder using Golang

Here is video step to step how to create html builder in golang

package html

type Node interface {
Html() string
}

Implement Node in Div tag

package html

type DIV struct {
Child []Node
Value string
}

func (d DIV) Html() string {
output := "<div>"
output += d.Value
if d.Child != nil {
for _, node := range d.Child {
output += node.Html()
}
}
output += "</div>"

return output
}

func (d *DIV) Add(node Node) *DIV {
/*
<div>
<div>child 1</div>
</div>
*/
d.Child = append(d.Child, node)
return d
}

Example

package main

import "github.com/tabvn/html"

func main() {
container := html.DIV{
Child: []html.Node{
html.DIV{Value: "This is content of child 1"},
},
}

container.Add(html.DIV{Value: "Child 2"})

ul := html.UL{
Child: []html.Node{
html.LI{Value: "List item 1"},
},
}
li := html.LI{Value: "List item 2"}
li.Add(html.A{Value: "Click here", URL: "http://drupalexp.com"})
ul.Add(li)
container.Add(ul)
println(container.Html())
}

https://www.youtube.com/watch?v=RdlTdvq0c2A

Github https://github.com/tabvn/html

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Tabvn
Tabvn

Written by Tabvn

i’m from a nice city in Vietnam called Danang, where live and did my studies. I love programming, coffee and spending my free time teaching myself new skills.

No responses yet

Write a response