KorHosik
KorHosik

Reputation: 1267

Go : concatenate file contents

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!

Upvotes: 3

Views: 4299

Answers (1)

peterSO
peterSO

Reputation: 166616

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

Upvotes: 2

Related Questions