无法在go lang中分配* gorm.DB

I'm trying to return a instance from the gorm.Open() return it i'm getting following error

controllers/db.go:34: cannot assign *gorm.DB to dc.DB (type gorm.DB) in multiple assignment

This is the db.go controller

    package controllers

import (

  //"fmt"

  _ "github.com/go-sql-driver/mysql"
  //v "github.com/spf13/viper"
  "github.com/jinzhu/gorm"

)

type DBController struct {
    DB gorm.DB
}

func (dc *DBController) InitDB() {
    var err error


    dc.DB, err = gorm.Open("mysql","root:12345@tcp(localhost:3306)/api")
    if err != nil {
        log.Fatalf("Error when connect database, the error is '%v'", err)
    }
    dc.DB.LogMode(true)
}

func (dc *DBController) GetDB() gorm.DB {
  return dc.DB
}

What is reason for this error and how can i fix this?

You need and most probably you want to have a pointer in the structure of controller. Passing structure with a pointer to the database object (gorm.DB) will prevent Go from making a copy of this object (gorm.DB).

Do the following:

type DBController struct {
    DB *gorm.DB
}

Now it should work fine.