2

I have two packages, main and db. However, I get "DB declared and not used" error.

db.go

package db

import (
  "database/sql"
)

var DB *sql.DB

func Connect() {
  DB, err := sql.Open("mysql", "root:xxxx@/xxxx")
  if err != nil {
    panic(err.Error())
  }
}

func Close() {
  DB.Close()
}

main.go

package main

import (
    "database/sql"
    // "fmt"
    _ "github.com/go-sql-driver/mysql"
    "html/template"
    "net/http"
  "github.com/****/****/config"
  "github.com/****/****/db"
)

var tpl *template.Template

func init() {
    tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
    Connect()
    defer Close()
    loadRoutes()
    http.ListenAndServe(":8080", nil)
}
1
  • 3
    The problem is that you are using :=, the short variable declaration, which declares a new DB variable for the scope of the Connect function. That means that the DB declared at the package level is a different variable from the DB declared inside Connect. To fix the problem, inside Connect just above sql.Open, declare an error variable (i.e. var err error) and then use =; i.e. DB, err = sql.Open(.... Example: play.golang.org/p/3XglGr-2LZa Commented Aug 18, 2018 at 16:53

2 Answers 2

3

Golang is strict about variable declaration, it is also mentioned in the Golang FAQs:

The presence of an unused variable may indicate a bug, while unused imports just slow down compilation, an effect that can become substantial as a program accumulates code and programmers over time. For these reasons, Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity.

It's easy to address the situation, though. Use the blank identifier to let unused things persist while you're developing.

_, err := sql.Open("mysql", "root:Berlin2018@/jplatform")

But since you want db instance by creating a connection. I suggest to use it either by returning from the function OR You can check for the connection if it is working or not by sending ping to the database server as:

var DB *sql.DB

func Connect() {
    DB, err := sql.Open("mysql", "root:Berlin2018@/jplatform")
    if err = DB.Ping(); err != nil {
        log.Panic(err)
    }
}

Or you can create a struct which you can use anywhere you want including use of method receiver for every function which require db connection for querying the database as

type Env struct {
    db *sql.DB
}

func Connect() {
    db, err := sql.Open("mysql", "root:Berlin2018@/jplatform")
    _ = &Env{db: db}
}

func(env *Env) getDataFromDatabase(){}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, @Himanshu! On the solution using the strct approach, though, how should I close the database connection? Would it be possible do defer it?
1

You are not using your DB variable on db.go:

package db

import (
  "database/sql"
)

var DB *sql.DB

func Connect() {
  con, err := sql.Open("mysql", "root:Berlin2018@/jplatform")
  if err != nil {
    panic(err.Error())
  }
  DB = con
}

func Close() {
  DB.Close()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.