drwg89980 2012-08-02 20:53
浏览 19
已采纳

转到:连接文件内容

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 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 := "/path/to/my/file.html"
mainFileDir := path.Dir(mainFilePath)+"/"

mainFileContent, err := ioutil.ReadFile(mainFilePath)

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

    //Start RegExp searching for JavaScript src
    scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
    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 "final" variable
            fmt.Println(finalFileContent.Write(subFileContent))
        } else {
            fmt.Println(err)
        }
    }

    //Try to display the final result
    // fmt.Println(finalFileContent.String())
    fmt.Printf(">>> %#v", finalFileContent)
    fmt.Println("Y U NO WORKS? :'(")

} else {
    fmt.Println(err)
}

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

But fmt.Printf(">>> %#v", 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条回答 默认 最新

  • dongqu3623 2012-08-02 23:35
    关注

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

    package main
    
    import (
        "bytes"
        "fmt"
        "io/ioutil"
        "path"
        "regexp"
    )
    
    func main() {
        //Open main file
        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
    
            //Start RegExp searching for JavaScript src
            scriptReg, _ := regexp.Compile("<script src=\"(.*)\">")
            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 "final" variable
    
                    // fmt.Println(finalFileContent.Write(subFileContent))
                    n, err := finalFileContent.Write(subFileContent)
                    fmt.Println("finalFileContent Write:", n, err)
    
                } else {
                    fmt.Println(err)
                }
            }
    
            //Try to display the final result
            // 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)
        }
    }
    

    UPDATE:

    The statement:

    fmt.Println("finalFileContent Printf:", 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致