Last active
October 24, 2024 13:47
-
-
Save kaihendry/9f6f48d0c7529933baeb81a30aede3ee to your computer and use it in GitHub Desktop.
Minimal HTMX 2.0 example in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"math/rand" | |
) | |
func main() { | |
const html = ` | |
<html> | |
<head> | |
<script src="https://unpkg.com/[email protected]" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script> | |
</head> | |
<body> | |
<div id="parent-div"> | |
<p>Say something</p> | |
</div> | |
<button hx-post="/clicked" | |
hx-trigger="click" | |
hx-target="#parent-div" | |
hx-swap="outerHTML" | |
> | |
Click Me! | |
</button> | |
</body> | |
</html> | |
` | |
http.HandleFunc("/clicked", func(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
greetings := []string{"hello", "hi", "howdy", "hola", "bonjour", "ciao", "hallo", "hej", "guten tag", "namaste", "ni hao", "salut", "merhaba", "こんにちは", "안녕하세요", "안녕", "你好", "नमस्ते", "П��ивет", "你好", "Olá", "Hallo", "Ciao", "Hola", "Bonjour", "Merhaba", "こんにちは", "안녕하세요", "안녕", "你好", "नमस्ते", "Привет", "你好", "Olá", "Hallo", "Ciao", "Hola", "Bonjour", "Merhaba", "こんにちは", "안녕하세요", "안녕", "你好", "नमस्ते", "Привет", "你好", "Olá", "Hallo", "Ciao", "Hola", "Bonjour", "Merhaba"} | |
randomGreeting := greetings[rand.Intn(len(greetings))] | |
w.Write([]byte("<div id=\"parent-div\"><h1>" + randomGreeting + "</h1></div>")) | |
}) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(html)) | |
}) | |
log.Println("starting server on port 8080") | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment