Go:连接文件内容

huangapple go评论92阅读模式
英文:

Go : concatenate file contents

问题

我目前正在学习如何使用Go(或golang)进行开发,我遇到了一个奇怪的问题:

我试图创建一个脚本,查找HTML文件中的所有**<script>**标签的源代码。
脚本的目标是合并所有获取到的文件。

以下是我的脚本:

//打开主文件
mainFilePath := "/path/to/my/file.html"
mainFileDir := path.Dir(mainFilePath)+"/"
	
mainFileContent, err := ioutil.ReadFile(mainFilePath)

if err == nil {
	mainFileContent := string(mainFileContent)
	var finalFileContent bytes.Buffer
		
	//开始使用正则表达式搜索JavaScript src
	scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
	scripts := scriptReg.FindAllStringSubmatch(mainFileContent,-1)
		
	//对于每个找到的SRC...
	for _, path := range scripts {
		//我们打开相应的文件
		subFileContent, err := ioutil.ReadFile(mainFileDir+path[1])
			
		if err == nil {
			//并将其内容添加到“final”变量中
			fmt.Println(finalFileContent.Write(subFileContent))
		} else {
			fmt.Println(err)
		}
	}
		
	//尝试显示最终结果
	// fmt.Println(finalFileContent.String())
	fmt.Printf(">>> %#v", finalFileContent)
    fmt.Println("Y U NO WORKS? :'(")
	
} else {
	fmt.Println(err)
}

所以,每个fmt.Println(finalFileContent.Write(subFileContent))显示类似于**6161 <nil>**的内容,所以我认为*Write()*方法被正确执行。

但是fmt.Printf(">>> %#v", finalFileContent)没有显示任何内容。绝对没有任何内容(甚至没有显示“>>>”!)上面的注释行也是一样的情况。

有趣的是,字符串*"Y U NO WORK ? :'(*正确显示...

你知道为什么吗?
你知道如何解决这个问题吗?

提前感谢!

英文:

I'm currently learning how to develop with Go (or golang) and I have a strange issue:

I try to create a script looking inside an HTML file in order to get all the sources of each <script> tags.
The goal of the script is to merge all the retrieved files.

So, that's for the story: for now, I'm able to get the content of each JavaScript files but... I can't concatenate them...

You can see below my script:

//Open main file
mainFilePath := &quot;/path/to/my/file.html&quot;
mainFileDir := path.Dir(mainFilePath)+&quot;/&quot;
	
mainFileContent, err := ioutil.ReadFile(mainFilePath)

if err == nil {
	mainFileContent := string(mainFileContent)
	var finalFileContent bytes.Buffer
		
	//Start RegExp searching for JavaScript src
	scriptReg, _ := regexp.Compile(&quot;&lt;script src=\&quot;(.*)\&quot;&gt;&quot;)
	scripts := scriptReg.FindAllStringSubmatch(mainFileContent,-1)
		
	//For each SRC found...
	for _, path := range scripts {
		//We open the corresponding file
		subFileContent, err := ioutil.ReadFile(mainFileDir+path[1])
			
		if err == nil {
			//And we add its content to the &quot;final&quot; variable
			fmt.Println(finalFileContent.Write(subFileContent))
		} else {
			fmt.Println(err)
		}
	}
		
	//Try to display the final result
	// fmt.Println(finalFileContent.String())
	fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
    fmt.Println(&quot;Y U NO WORKS? :&#39;(&quot;)
	
} else {
	fmt.Println(err)
}

So, each fmt.Println(finalFileContent.Write(subFileContent)) display something like 6161 <nil>, so I assume the Write() method is correctly executed.

But fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent) displays nothing. Absolutely nothing (even the ">>>" are not displayed!) And it's the same for the commented line just above.

The funny part is that the string "Y U NO WORK ? :'(" is correctly displayed...

Do you know why?
And do you know how to solve this issue?

Thanks in advance!

答案1

得分: 2

你忽略了一些错误。当你运行以下版本的代码时,你的结果是什么?

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"path"
	"regexp"
)

func main() {
	//打开主文件
	mainFilePath := "/path/to/my/file.html"
	mainFileDir := path.Dir(mainFilePath) + "/"

	mainFileContent, err := ioutil.ReadFile(mainFilePath)

	if err == nil {
		mainFileContent := string(mainFileContent)
		var finalFileContent bytes.Buffer

		//开始使用正则表达式搜索JavaScript src
		scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
		scripts := scriptReg.FindAllStringSubmatch(mainFileContent, -1)

		//对于每个找到的SRC...
		for _, path := range scripts {
			//我们打开相应的文件
			subFileContent, err := ioutil.ReadFile(mainFileDir + path[1])

			if err == nil {
				//并将其内容添加到“final”变量中

				// fmt.Println(finalFileContent.Write(subFileContent))
				n, err := finalFileContent.Write(subFileContent)
				fmt.Println("finalFileContent Write:", n, err)

			} else {
				fmt.Println(err)
			}
		}

		//尝试显示最终结果
		// fmt.Println(finalFileContent.String())

		// fmt.Printf(">>> %#v", finalFileContent)
		n, err := fmt.Printf(">>> %#v", finalFileContent)
		fmt.Println()
		fmt.Println("finalFileContent Printf:", n, err)

		fmt.Println("Y U NO WORKS? :'(")

	} else {
		fmt.Println(err)
	}
}

更新:

语句:

fmt.Println("finalFileContent Printf:", n, err)

输出:

finalFileContent Printf: 0 write /dev/stdout: winapi error #8

或者

finalFileContent Printf: 0 write /dev/stdout: Not enough storage is available to process this command.

来自MSDN:

> ERROR_NOT_ENOUGH_MEMORY
>
> 8 (0x8)
>
> Not enough storage is available to process this command.

格式化的输出超出了Windows控制台的缓冲区(约64KB)。

这是一个相关的Go开放问题:

> Issue 3376: windows: detect + handle console in os.File.Write

英文:

You are ignoring some errors. What are your results when you run the following version of your code?

package main

import (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;path&quot;
	&quot;regexp&quot;
)

func main() {
	//Open main file
	mainFilePath := &quot;/path/to/my/file.html&quot;
	mainFileDir := path.Dir(mainFilePath) + &quot;/&quot;

	mainFileContent, err := ioutil.ReadFile(mainFilePath)

	if err == nil {
		mainFileContent := string(mainFileContent)
		var finalFileContent bytes.Buffer

		//Start RegExp searching for JavaScript src
		scriptReg, _ := regexp.Compile(&quot;&lt;script src=\&quot;(.*)\&quot;&gt;&quot;)
		scripts := scriptReg.FindAllStringSubmatch(mainFileContent, -1)

		//For each SRC found...
		for _, path := range scripts {
			//We open the corresponding file
			subFileContent, err := ioutil.ReadFile(mainFileDir + path[1])

			if err == nil {
				//And we add its content to the &quot;final&quot; variable

				// fmt.Println(finalFileContent.Write(subFileContent))
				n, err := finalFileContent.Write(subFileContent)
				fmt.Println(&quot;finalFileContent Write:&quot;, n, err)

			} else {
				fmt.Println(err)
			}
		}

		//Try to display the final result
		// fmt.Println(finalFileContent.String())

		// fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
		n, err := fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
		fmt.Println()
		fmt.Println(&quot;finalFileContent Printf:&quot;, n, err)

		fmt.Println(&quot;Y U NO WORKS? :&#39;(&quot;)

	} else {
		fmt.Println(err)
	}
}

UPDATE:

The statement:

fmt.Println(&quot;finalFileContent Printf:&quot;, n, err)

Outputs:

finalFileContent Printf: 0 write /dev/stdout: winapi error #8

or

finalFileContent Printf: 0 write /dev/stdout: Not enough storage is available to process this command.

From MSDN:

> ERROR_NOT_ENOUGH_MEMORY
>
> 8 (0x8)
>
> Not enough storage is available to process this command.

The formatted output to the Windows console overflows the buffer (circa 64KB).

There is a related Go open issue:

> Issue 3376: windows: detect + handle console in os.File.Write

huangapple
  • 本文由 发表于 2012年8月3日 04:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/11785590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定