Original post

Hi,

I’m working on creating an export program that reads a mysql query and then exports the results into a generated csv file.

The issue Im running into is, I’ve created 50+ files that all connect to the DB. Ideally I don’t want to through each of those files and change my sql.Open() credentials in each file every time I need to point to a new environment.

Is there a way that I can create a globally set func or var that can handle this change once instead of having to go through each file?

func ExportQuery() {
        fmt.Println("Test for MySQL")

        db, _ := sql.Open()

rows, _ := db.Query("SELECT * FROM table WHERE id = ?;", os.Args)

        err := sqltocsv.WriteFile("table.csv",rows)
        if err != nil {
                panic(err)
        }

        columns, _ := rows.Columns()
        count := len(columns)
        values := make([]interface{}, count)
        valuePtrs := make([]interface{}, count)

        for rows.Next() {
                for i := range columns {
                        valuePtrs[i] = &values[i]
                }

                rows.Scan(valuePtrs...)

                for i, col := range columns {
                        val := values[i]

                        b, ok := val.([]byte)
                        var v interface{}
                        if ok {
                                v = string(b)
                        } else {
                                v = val
                        }

                        fmt.Println(col, v)
                }
        }
}