turbo-go icon indicating copy to clipboard operation
turbo-go copied to clipboard

Build hotwire apps using go

Turbo-go

Build hotwire applications using Go

Example

Examples are in examples directory

Install turbo-go

go get github.com/akmittal/turbo-go

API

github.com/akmittal/turbo-go/pkg/turbo

Send single template update

messageTemp, err := template.New("message").parse(`<div>{{.}}</div>`)
data := time.Now()
turbo := turbo.Turbo{
		Action:   turbo.APPEND, // Action can be UPDATE, APPEND, PREPEND, REPLACE, REMOVE
		Template: messageTemp,
		Target:   "messages",
		Data:     data,
	}

Send stream of templates

func main(){
	// Create hub
    hub := turbo.NewHub()
    go hub.Run()
    mux.Get("/socket", func(rw http.ResponseWriter, req *http.Request) {
		getSocket(msgChan, hub, rw, req)
	})
}

func getSocket(msgChan chan interface{}, hub *turbo.Hub, rw http.ResponseWriter, req *http.Request) {
	temp, _ := template.ParseFiles("templates/messages.temp.html")
	messageTemp := temp.Lookup("message")

	appendMessage := turbo.Stream{
		Action:   turbo.APPEND,
		Template: messageTemp,
		Target:   "messages",
		Data:     msgChan,
	}

	appendMessage.Stream(hub, rw, req)
}