go模板文件引入js路径问题

Python014

go模板文件引入js路径问题,第1张

Go语言模板文件可以引入js文件或css文件,但是在引入的过程中,需要注意以下几点:

1. 引入的文件路径应该是相对路径,而不是绝对路径。

2. 在引入js文件时,需要使用{{ url }} 模板函数,用来拼接路径, 这样可以更好的兼容不同的路径。

3. 如果是在统一的文件夹中的js文件,最好使用{{ static }}模板函数,这样可以更好的节省路径长度。

4. 在引用js文件时,需要在页面底部,可以使用{{ template }}模板函数,这样可以保证js文件在页面加载完成之前就被加载。

总之,使用Go语言模板文件引入js文件,需要注意路径的相对性,并且使用模板函数来拼接路径,这样可以更好的兼容不同的路径,从而保证引用js文件的正确性。

使用go命令可以定位到文件头和文件末

当go run 运行时会将文件转移到临时路径下,然后再进行编译和执行,分片0就是C:\Users\xxx\AppData\Local\Temp___go_build_main_go.exe;

go build编译执行时,那么分片0为执行文件的相对路径为(main.go)

fmt.Println(os.Args)

//output:[C:\Users\xxx\AppData\Local\Temp\___go_build_main_go.exe]

1

2

1

2

exec.LookPath()

根据传入的参数来从PATH中获取可执行文件的绝对路径(没有编译的)或者相对路径(编译后的);参数若带分割号就直接查询返回

func LookPath(file string) (string, error)

LookPath searches for an executable named file in the directories named by the

PATH environment variable. If file contains a slash, it is tried directly and the

PATH is not consulted. LookPath also uses PATHEXT environment variable to match a

suitable candidate. The result may be an absolute path or a path relative to the

current directory.

1

2

3

4

5

6

7

1

2

3

4

5

6

7

filepath.Abs()

根据传入的路径计算出绝对路径,如果传入的为相对路径,那么它会把当前路径拼接上

编译后返回的是真实的路径(D:\go_project\src\github.com\wzbwzt\studyGo\GetPath\main.exe)

未编译执行返回的是临时执行路径

(C:\Users\xxx\AppData\Local\Temp___go_build_main_go.exe)

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:E:\opensource\go\go├─api├─bin│├─go.exe│├─godoc.exe│└─gofmt.exe├─doc├─include├─lib├─misc├─pkg├─src└─test2、增加环境变量GOROOT,取值为上面的go工作目录3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了E:\opensource\go\go>goGoisatoolformanagingGosourcecode.Usage:gocommand[arguments]Thecommandsare:buildcompilepackagesanddependenciescleanremoveobjectfilesdocrungodoconpackagesourcesenvprintGoenvironmentinformationfixrungotoolfixonpackagesfmtrungofmtonpackagesourcesgetdownloadandinstallpackagesanddependenciesinstallcompileandinstallpackagesanddependencieslistlistpackagesruncompileandrunGoprogramtesttestpackagestoolrunspecifiedgotoolversionprintGoversionvetrungotoolvetonpackagesUse"gohelp[command]"formoreinformationaboutacommand.Additionalhelptopics:gopathGOPATHenvironmentvariablepackagesdescriptionofpackagelistsremoteremoteimportpathsyntaxtestflagdescriptionoftestingflagstestfuncdescriptionoftestingfunctionsUse"gohelp[topic]"formoreinformationaboutthattopic.5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一helloworld.go",直接调用"gobuildhelloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。E:\opensource\go\go\test>gobuildhelloworld.goE:\opensource\go\go\test>helloworld.exehello,worldE:\opensource\go\go\test>附一helloworld.go//cmpout//Copyright2009TheGoAuthors.Allrightsreserved.//UseofthissourcecodeisgovernedbyaBSD-style//licensethatcanbefoundintheLICENSEfile.//Testthatwecandopage1oftheCbook.packagemainfuncmain(){print("hello,world\n")}