在 Go 模板中包含 js 文件

我最近开始学习围棋。我得到了一个像 web 应用程序一样的示例。我有:


/* tick-tock.go */

package main


import (

    "fmt"

    "io/ioutil"

    "log"

    "net/http"

)


// Content for the main html page..

var page = `<html>

           <head>

             <script type="text/javascript"

               src="http://localhost:8081/jquery.min.js">

             </script>

             <style> 

               div {

                 font-family: "Times New Roman", Georgia, Serif;

                 font-size: 1em;

                 width: 13.3em;

                 padding: 8px 8px; 

                 border: 2px solid #2B1B17;

                 color: #2B1B17;

                 text-shadow: 1px 1px #E5E4E2;

                 background: #FFFFFF;

               }

             </style>

           </head>

           <body>

             <h2 align=center>Go Timer </h2>

             <div id="output" style="width: 30%; height: 63%; overflow-y: scroll; float:left;"></div>

             <div id="v1" style="width: 50%; height: 30%; overflow-y: scroll; float:left;"></div>

             <div id="v2" style="width: 50%; height: 30%; overflow-y: scroll; float:left;"></div>

             <input id="sett" type="submit" name="sett" value="Settings" onclick="changeUrl()">


             <script type="text/javascript">


               var myDelay;


               $(document).ready(function () 

               {

                   $("#output").append("Waiting for system time..");


                   myDelay = setInterval("delayedPost()", 1000);               


                });


               function delayedPost() 

               {

                 $.post("http://localhost:9999/dev", "", function(data, status) 

                 {

                    //$("#output").empty();

                    $("#output").prepend(data);

                 });

我无法加载本地jquery.min.js. 我写的src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"时候已经加载了。如何加载本地js文件?我不擅长用 Go 编写代码,也没有编写完整的代码。所以请尽量解释得很简单。提前致谢!


慕工程0101907
浏览 273回答 1
1回答

catspeake

您需要 aHandler或 a在请求时HandlerFunc将文件内容 ( jquery.min.js) 发送到 Web 浏览器。您有 3 个选择:手动操作这是更复杂的解决方案。它看起来就像在您的处理程序函数中读取文件的内容,设置正确的响应内容类型 ( application/javascript) 并将内容(即 a []byte)发送到响应。注意事项:读取文件时,必须指定绝对路径。如果您指定相对路径,请确保该文件位于您启动应用程序的当前文件夹(工作目录)中。例子:func SendJqueryJs(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; data, err := ioutil.ReadFile("jquery.min.js")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, "Couldn't read file", http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; w.Header().Set("Content-Type", "application/javascript; charset=utf-8")&nbsp; &nbsp; w.Write(data)}func main() {&nbsp; &nbsp; http.HandleFunc("/jquery.min.js", SendJqueryJs)&nbsp; &nbsp; panic(http.ListenAndServe(":8081", nil))}上面的例子只能提供 1 个文件:jquery.min.js对于请求:http://localhost:8081/jquery.min.js利用 http.ServeFile()这更容易:该函数http.ServeFile()能够将一个文件的内容发送到指定的响应。您仍然需要创建一个函数或处理程序来使用它,但它会为您完成剩下的工作:func SendJqueryJs(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; http.ServeFile(w, r, "jquery.min.js")}利用 http.FileServer()如果您需要提供多个静态文件,这就是FileServer()函数派上用场的地方,它返回一个Handler自动为您本地文件系统提供的文件,这些文件是您指定的根文件夹的后代。这种解决方案更加灵活:它可以发送多种类型的多个文件,自动检测和设置内容类型。处理程序还能够呈现 HTML 页面,以列出带有文件链接和父/子文件夹链接的目录内容。例子:http.Handle("/tmpfiles/",&nbsp; &nbsp; http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))这将Handler在 URL 上注册一个,该 URL/tmpfiles/提供在/tmp文件夹中本地文件系统中找到的文件。因此,例如以下<script>链接:<script type="text/javascript" src="/tmpfiles/jquery.min.js">/tmp/jsquery.min.js将从服务器获取文件。查看此答案,其中详细介绍了如何使用/启动静态文件服务器。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go