Skip to content

Go

This guide will show how to use hop in a Go backend.

We start by writing a simple hop file.

hop
<page-index {name: String}>
  Hello {name}!
</page-index>

Now we compile the hop code to native Go code.

bash
hop compile go

Now the generated Go code is available in the folder ./frontend so we can import it in the backend.

Now, let's define a simple backend using the standard library.

go
package main

import (
    "net/http"
    "frontend"
)

func main() {
    router := http.NewServeMux()

    router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
        frontend.PageIndex(w, frontend.PageIndexParams {
            name: "Tobi"
        })
    })

    http.ListenAndServe(":8080", router)
}

Now hop is integrated into the backend, and we can run hop dev to start the server and get hot-reloading.