Newbie question, "declared not used"

So I’ve been following the nice link on using go on Heroku.
I have to ask,since I’m kind of afraid I’m just stupid or perhaps need a lot more coffee:

Does that actually work? All I get is:

remote: cmd/go-getting-started/main.go:60: db declared and not used
remote: godep: go exit status 2
remote: ! Push rejected, failed to compile Go app

Now… some more coffee and “GoBootcamp.Pdf”

It means you declare the variable “db” (at line 60), and you don’t use it.
It’s an error in Go.

Can you show your code?

1 Like

Hi and thanks. Yes that’s what i thought but as far as I can see I’ve been following their code in that link.
I declare the “db” in func main() and use it in dbFunc() ??

In their code:

db is declared here:

var (
    repeat int
    db     *sql.DB = nil

It is assigned here:

db, errd = sql.Open("postgres", os.Getenv("DATABASE_URL"))

Note, they use = and not :=

It is use here (in dbFunc()):

if _, err := db.Exec("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); err != nil

Aaah, of course… sloppiness as usual, thank you for pointing that out to me. So I was kind of stupid after all :wink:

Well, that’s a very common mistake, so you are not stupid :), you are just learning…


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.