package main import ( "embed" "fmt" "math/rand" "net/http" "strings" ) //go:embed facts.txt var facts embed.FS func loadFacts() ([]string, error) { // Read the embedded file data, err := facts.ReadFile("facts.txt") if err != nil { return nil, err } // Split the file into lines and return as a slice lines := string(data) return splitLines(lines), nil } func splitLines(data string) []string { return strings.Split(strings.TrimSpace(data), "\n") } func randomFactHandler(facts []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Get a random index index := rand.Intn(len(facts)) // Write the random fact as an HTML response with CSS fmt.Fprintf(w, ` Random Fact

%s

`, facts[index]) } } func main() { // Load facts from the embedded file facts, err := loadFacts() if err != nil { panic(err) } // Set up the HTTP server http.HandleFunc("/", randomFactHandler(facts)) fmt.Println("Server is running on http://localhost:8080") err = http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }