I wrote a program that creates a .sql file, changes directories one layer down, reads a bunch of individual .txt files in that directory, and then writes the content from those .txt files to the .sql file. It runs fine and creates the .sql file, but rather than write the intended content, it writes a bunch of numbers (memory locations?). I'm stumped because if I do a fmt.Println using the same variable, it gives the correct(mostly... work in progress) output. On top of that if I write the exact same content to another .txt file rather than a .sql, it's output is different than the .sql's output.
Here is afew lines that get written to the .sql file
0000 025e 0000 02c8 ffff ffff ffff 0000 0061 006c 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
Here is what is output in the terminal via Println and what is written into .txt files (what is supposed to be output in the .sql files.
----------------------------
Employee: John_Smith
Username: john
Email: john@somecompany.com
Location: HQ-thatplace
Department: QA
Manager: Jim_Smith
----------------------------
----------------------------
Employee: John_Smith2
Username: john2
Email: john2@somecompany.com
Location: HQ-thatplace
Department: Support
Manager: Jim_Smith2
----------------------------
I would appreciate any insight on this problem, whether its a syntax issue, an issue with how I'm trying to use .sql, or something else. Thanks!
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
)
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func changeDirectory() []os.FileInfo {
files, _ := ioutil.ReadDir("./theList")
err := os.Chdir("./theList")
if err != nil {
panic(err)
}
return files
}
func main() {
content, err := os.Create("textToSQLOutput.sql")
if err != nil {
panic(fmt.Errorf("1: %v
", err))
}
defer content.Close()
files, _ := ioutil.ReadDir("./theList")
changeDirectory()
for _, f := range files {
lines, err := readLines(f.Name())
if err != nil {
log.Fatal(err)
}
for _, line := range lines {
//fmt.Println(line)
if _, err = content.WriteString(line); err != nil {
panic(fmt.Errorf("2: %v
", err))
}
}
}
}