Skip to content

Go

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

We start by creating a hop.toml file.

toml
[compile]
target = "go"
output_path = "frontend/frontend.go"

[dev]
server_commands = [
  "go build",
  "./server",
]

We continue by creating a file called index.hop.

hop
<Index {name: String}>
  Hello {name}!
</Index>

Now we compile the hop code to Go.

bash
hop compile

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 in a file called main.go.

go
package main

import (
    "net/http"
    "frontend"
)

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

    router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
        frontend.Index(w, frontend.IndexParams {
            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.