Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 在html中包含css,我应该将css文件放在哪里?_Go - Fatal编程技术网

Go 在html中包含css,我应该将css文件放在哪里?

Go 在html中包含css,我应该将css文件放在哪里?,go,Go,我试图将css包含到html中,但没有成功。我在同一目录/测试中有可执行文件html和CSS 我对这个问题做了一些研究,但我仍然不能以适当的方式包括css。对于我已经看到的内容,如果我包含以/it开头的css文件,它是相对于根文件夹的,以便确认我打印的程序的根文件夹在哪里,它指向C:\Users\Filipe\Desktop\go\src\test>我的所有文件在哪里 test.go: package main import ( "fmt" "ne

我试图将css包含到html中,但没有成功。我在同一目录/测试中有可执行文件html和CSS

我对这个问题做了一些研究,但我仍然不能以适当的方式包括css。对于我已经看到的内容,如果我包含以/it开头的css文件,它是相对于根文件夹的,以便确认我打印的程序的根文件夹在哪里,它指向C:\Users\Filipe\Desktop\go\src\test>我的所有文件在哪里

test.go:

package main

import (
    "fmt"
    "net/http"
    "os"
    "text/template"
)

type Page struct {
    Title    string
    NavItems []navItem
}

type navItem struct {
    Item string
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    page := Page{
        Title: "title",
        NavItems: []navItem{
            {Item: "item1"},
            {Item: "item2"},
        },
    }
    t, _ := template.ParseFiles("index.html")
    t.Execute(w, page)
}

func testHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "/t directory test")
}

func main() {
    http.HandleFunc("/", indexHandler)
    fmt.Println(os.Getwd())

    http.ListenAndServe(":8080", nil)
}
index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="/mystyle.css">
    <title>{{.Title}}</title>
</head>
<body>
    <nav>
    {{range .NavItems}}
        <a>{{.Item}}<a>
    {{end}}
    </nav>
</body>
</html>
尝试改变-

   <link rel="stylesheet" type="text/css" href="/mystyle.css">


这将解决问题。

您忘记注册将为静态文件提供服务的处理程序。只要搜索一下如何使用Go服务静态文件,就会有很多答案、博客帖子等。好的,谢谢你的指导,这应该足以开始搜索并解决我的问题
<link rel="stylesheet" type="text/css" href="test/mystyle.css">