Go:连接文件内容

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

Go : concatenate file contents

问题

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

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

以下是我的脚本:

  1. //打开主文件
  2. mainFilePath := "/path/to/my/file.html"
  3. mainFileDir := path.Dir(mainFilePath)+"/"
  4. mainFileContent, err := ioutil.ReadFile(mainFilePath)
  5. if err == nil {
  6. mainFileContent := string(mainFileContent)
  7. var finalFileContent bytes.Buffer
  8. //开始使用正则表达式搜索JavaScript src
  9. scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
  10. scripts := scriptReg.FindAllStringSubmatch(mainFileContent,-1)
  11. //对于每个找到的SRC...
  12. for _, path := range scripts {
  13. //我们打开相应的文件
  14. subFileContent, err := ioutil.ReadFile(mainFileDir+path[1])
  15. if err == nil {
  16. //并将其内容添加到“final”变量中
  17. fmt.Println(finalFileContent.Write(subFileContent))
  18. } else {
  19. fmt.Println(err)
  20. }
  21. }
  22. //尝试显示最终结果
  23. // fmt.Println(finalFileContent.String())
  24. fmt.Printf(">>> %#v", finalFileContent)
  25. fmt.Println("Y U NO WORKS? :'(")
  26. } else {
  27. fmt.Println(err)
  28. }

所以,每个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:

  1. //Open main file
  2. mainFilePath := &quot;/path/to/my/file.html&quot;
  3. mainFileDir := path.Dir(mainFilePath)+&quot;/&quot;
  4. mainFileContent, err := ioutil.ReadFile(mainFilePath)
  5. if err == nil {
  6. mainFileContent := string(mainFileContent)
  7. var finalFileContent bytes.Buffer
  8. //Start RegExp searching for JavaScript src
  9. scriptReg, _ := regexp.Compile(&quot;&lt;script src=\&quot;(.*)\&quot;&gt;&quot;)
  10. scripts := scriptReg.FindAllStringSubmatch(mainFileContent,-1)
  11. //For each SRC found...
  12. for _, path := range scripts {
  13. //We open the corresponding file
  14. subFileContent, err := ioutil.ReadFile(mainFileDir+path[1])
  15. if err == nil {
  16. //And we add its content to the &quot;final&quot; variable
  17. fmt.Println(finalFileContent.Write(subFileContent))
  18. } else {
  19. fmt.Println(err)
  20. }
  21. }
  22. //Try to display the final result
  23. // fmt.Println(finalFileContent.String())
  24. fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
  25. fmt.Println(&quot;Y U NO WORKS? :&#39;(&quot;)
  26. } else {
  27. fmt.Println(err)
  28. }

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

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

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "path"
  7. "regexp"
  8. )
  9. func main() {
  10. //打开主文件
  11. mainFilePath := "/path/to/my/file.html"
  12. mainFileDir := path.Dir(mainFilePath) + "/"
  13. mainFileContent, err := ioutil.ReadFile(mainFilePath)
  14. if err == nil {
  15. mainFileContent := string(mainFileContent)
  16. var finalFileContent bytes.Buffer
  17. //开始使用正则表达式搜索JavaScript src
  18. scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
  19. scripts := scriptReg.FindAllStringSubmatch(mainFileContent, -1)
  20. //对于每个找到的SRC...
  21. for _, path := range scripts {
  22. //我们打开相应的文件
  23. subFileContent, err := ioutil.ReadFile(mainFileDir + path[1])
  24. if err == nil {
  25. //并将其内容添加到“final”变量中
  26. // fmt.Println(finalFileContent.Write(subFileContent))
  27. n, err := finalFileContent.Write(subFileContent)
  28. fmt.Println("finalFileContent Write:", n, err)
  29. } else {
  30. fmt.Println(err)
  31. }
  32. }
  33. //尝试显示最终结果
  34. // fmt.Println(finalFileContent.String())
  35. // fmt.Printf(">>> %#v", finalFileContent)
  36. n, err := fmt.Printf(">>> %#v", finalFileContent)
  37. fmt.Println()
  38. fmt.Println("finalFileContent Printf:", n, err)
  39. fmt.Println("Y U NO WORKS? :'(")
  40. } else {
  41. fmt.Println(err)
  42. }
  43. }

更新:

语句:

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

输出:

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

或者

  1. 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?

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. &quot;path&quot;
  7. &quot;regexp&quot;
  8. )
  9. func main() {
  10. //Open main file
  11. mainFilePath := &quot;/path/to/my/file.html&quot;
  12. mainFileDir := path.Dir(mainFilePath) + &quot;/&quot;
  13. mainFileContent, err := ioutil.ReadFile(mainFilePath)
  14. if err == nil {
  15. mainFileContent := string(mainFileContent)
  16. var finalFileContent bytes.Buffer
  17. //Start RegExp searching for JavaScript src
  18. scriptReg, _ := regexp.Compile(&quot;&lt;script src=\&quot;(.*)\&quot;&gt;&quot;)
  19. scripts := scriptReg.FindAllStringSubmatch(mainFileContent, -1)
  20. //For each SRC found...
  21. for _, path := range scripts {
  22. //We open the corresponding file
  23. subFileContent, err := ioutil.ReadFile(mainFileDir + path[1])
  24. if err == nil {
  25. //And we add its content to the &quot;final&quot; variable
  26. // fmt.Println(finalFileContent.Write(subFileContent))
  27. n, err := finalFileContent.Write(subFileContent)
  28. fmt.Println(&quot;finalFileContent Write:&quot;, n, err)
  29. } else {
  30. fmt.Println(err)
  31. }
  32. }
  33. //Try to display the final result
  34. // fmt.Println(finalFileContent.String())
  35. // fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
  36. n, err := fmt.Printf(&quot;&gt;&gt;&gt; %#v&quot;, finalFileContent)
  37. fmt.Println()
  38. fmt.Println(&quot;finalFileContent Printf:&quot;, n, err)
  39. fmt.Println(&quot;Y U NO WORKS? :&#39;(&quot;)
  40. } else {
  41. fmt.Println(err)
  42. }
  43. }

UPDATE:

The statement:

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

Outputs:

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

or

  1. 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:

确定