Include js file in Go template

I recently started learning Go. I have a sample, for example, as a web application. I have:

/* 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);
                 });

                 $.post("http://localhost:9999/v1", "", function(data, status) {
                    //$("#output").empty();
                    $("#v1").prepend(data);
                 });

                 $.post("http://localhost:9999/v2", "", function(data, status) {
                    //$("#output").empty();
                    $("#v2").prepend(data);
                 });
               }

               function delayedPost1() 
               {
                 $.post("http://localhost:9999/dev", "", function(data, status) 
                 {                    
                    $("#output").prepend(data);
                 });

                 $.post("http://localhost:9999/v1", "", function(data, status) 
                 {                   
                    $("#v1").prepend(data);
                 });

                 $.post("http://localhost:9999/v3", "", function(data, status) 
                 {                    
                    $("#v2").prepend(data);
                 });
               }

               function changeUrl()
               {
                  alert('salom');                      
                  clearInterval(myDelay);



               }

             </script>
           </body>
         </html>`

// handler for the main page.
func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, page)
}

// handler to cater AJAX requests
func handlerDevs(w http.ResponseWriter, r *http.Request) {
    //fmt.Fprint(w, time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"))
    fmt.Fprint(w, "<font color=red>Dev1<br></font>")
}

func handlerV1(w http.ResponseWriter, r *http.Request) {
    //fmt.Fprint(w, time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"))
    fmt.Fprint(w, "<font color=blue>Vertical1<br></font>")
}

func handlerV2(w http.ResponseWriter, r *http.Request) {
    //fmt.Fprint(w, time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"))
    fmt.Fprint(w, "<font color=green>Vertical2<br></font>")
}

func main() {
    http.HandleFunc("/", handler)
    http.HandleFunc("/dev", handlerDevs)
    http.HandleFunc("/v1", handlerV1)
    http.HandleFunc("/v2", handlerV2)
    log.Fatal(http.ListenAndServe(":9999", nil))
    http.HandleFunc("/jquery.min.js", SendJqueryJs)
    panic(http.ListenAndServe(":8081", nil))
}
func SendJqueryJs(w http.ResponseWriter, r *http.Request) {
    data, err := ioutil.ReadFile("jquery.min.js")
    if err != nil {
        http.Error(w, "Couldn't read file", http.StatusInternalServerError)
        return
    }
    w.Header().Set("Content-Type", "application/javascript")
    w.Write(data)
}

I could not load local jquery.min.js. When I wrote src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js", it was uploaded. How to download local js file? I am not good at coding in Go, and I have not written the full code. Therefore, please try to explain very simply. Thanks in advance!

+4
source share
1 answer

You need Handleror HandlerFunc, which, on request, sends the contents of the file ( jquery.min.js) to a web browser.

You have 3 options:

Manual execution

. , , (application/javascript) ( []byte) .

: . , , ( ), .

:

func SendJqueryJs(w http.ResponseWriter, r *http.Request) {
    data, err := ioutil.ReadFile("jquery.min.js")
    if err != nil {
        http.Error(w, "Couldn't read file", http.StatusInternalServerError)
        return
    }
    w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
    w.Write(data)
}

func main() {
    http.HandleFunc("/jquery.min.js", SendJqueryJs)
    panic(http.ListenAndServe(":8081", nil))
}

: jquery.min.js :

http://localhost:8081/jquery.min.js

http.ServeFile()

: http.ServeFile() . , , :

func SendJqueryJs(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "jquery.min.js")
}

http.FileServer()

, FileServer() , Handler, , .

: , . HTML / .

:

http.Handle("/tmpfiles/",
    http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

Handler URL /tmpfiles/, , /tmp. , <script>:

<script type="text/javascript" src="/tmpfiles/jquery.min.js">

/tmp/jsquery.min.js .

, / .

+9

Source: https://habr.com/ru/post/1016743/


All Articles