Tuesday, October 8, 2024
HomeGolangWebpage is empty, I dont know learn how to modify in code...

Webpage is empty, I dont know learn how to modify in code – Getting Assist


foremost.go

package deal foremost

import (
	"fmt"
	"html/template"
	"log"
	"web/http"
	"os"
	"strings"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
	var lang = []string{"en", "zh", "es"}
	// 读取Accept-Language头部
	// acceptLanguage := r.Header.Get("Settle for-Language")

	// var langCode string

	// langCode = strings.Cut up(acceptLanguage, ",")[0][:2]
	langCode := strings.Cut up(r.Header.Get("Settle for-Language"), ",")[0][:2]
	// fmt.Println("Attempting language", langCode)

	discovered := false
	for _, l := vary lang {
		if l == langCode {
			discovered = true
			break
		}
	}
	if !discovered {
		langCode = "en"
	}

	// dir, err := os.Getwd()
	// if err != nil {
	// 	log.Fatalf("Did not get working listing: %v", err)
	// }
	// fmt.Println("Present Working Listing:", dir)

	url := "public/tmpl/" + langCode + "https://discussion board.golangbridge.org/"
	templates := []string{url + "index.html", url + "submenu.html", url + "content material.html"}

	// url := filepath.Be a part of(dir, "public/tmpl/", langCode)
	// 定义模板文件列表
	// templates := []string{url + "/index.html", url + "/submenu.html", url + "/content material.html"}
	// fmt.Println(templates)
	// templates := []string{
	// 	filepath.Be a part of(dir, "public/tmpl", langCode, "index.html"),
	// 	filepath.Be a part of(dir, "public/tmpl", langCode, "submenu.html"),
	// 	filepath.Be a part of(dir, "public/tmpl", langCode, "content material.html"),
	// }

	// 验证模板文件是否存在
	for _, file := vary templates {
		if _, err := os.Stat(file); os.IsNotExist(err) {
			log.Fatalf("Template file %s not discovered.", file)
		}
	}

	// fmt.Println(templates)

	// 加载模板
	tmpl := template.Should(template.ParseFiles(templates...))
	fmt.Println("Loaded templates:", tmpl.Templates())

	knowledge := struct {
		Title string
	}{
		Title: "我的主页",
	}

	if err := tmpl.ExecuteTemplate(w, templates[0], knowledge); err != nil {
		log.Printf("Error executing template: %v", err)
		// http.Error(w, "Error executing template", http.StatusInternalServerError)
		return
	}
}

func foremost() {
	http.HandleFunc("https://discussion board.golangbridge.org/", homeHandler)

	//启动服务器
	// fmt.Println("Server is listening on :8080")
	log.Println("Beginning server on :8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Deadly("Couldn't begin server: %v", err)
	}
}

index.html

{{ outline "index" }}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ .Title }}</title>
</head>
<physique>
    {{ template "submenu" . }}
    {{ template "content material" . }}
</physique>
</html>
{{ finish }}

content material.html

{{ outline "content material" }}
<foremost>
    <!-- 正文内容 -->
    <h1>嗨,欢迎你来访问我的个人主页,谢谢!</h1>
</foremost>
{{ finish }}

submenu.html

{{ outline "submenu" }}
<apart>
    <!-- 子菜单内容 -->
    <h1>首页、关于我、计算机、音乐、旅游</h1>
</apart>
{{ finish }}

I acquired error under:

go run "/Customers/douxiaobo/Paperwork/Observe in Coding/Personal_Website_Go/foremost.go"
Loaded templates: [0x14000183350 0x140001835f0 0x14000183620 0x14000183740 0x14000183770 0x140001838c0]
2024/07/05 16:45:40 Error executing template: html/template: "/Customers/douxiaobo/Paperwork/Observe in Coding/Personal_Website_Go/public/tmpl/zh/index.html" is undefined
Loaded templates: [0x140001d6150 0x14000183b60 0x14000183e00 0x14000183e30 0x14000183f50 0x140001d6000]
2024/07/05 16:45:40 Error executing template: html/template: "/Customers/douxiaobo/Paperwork/Observe in Coding/Personal_Website_Go/public/tmpl/zh/index.html" is undefined
^Csignal: interrupt
douxiaobo@192 Personal_Website_Go % 

Please inform me learn how to modify within the code. Thanks very a lot.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments