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.
Related
mac highsierra
use mysql
https://paiza.hatenablog.com/entry/2018/03/23/paizacloud_golang_revel)
I'm trying to use the Revel framework for mysql.
It's revel Tutorial booking.
what is error?
ERROR 18:33:28 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/gorm.go:31): undefined: sql"
before
ERROR 18:18:26 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/app.go:26): c.Txn undefined (type Application has no field or method Txn)"
I did this because I got an error before
controllers/gorm.go
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
After running it results in error
Please tell me how you can solve it
add
controllers/app.go
package controllers
import (
"github.com/revel/revel"
"booking/app/models"
"booking/app/routes"
"database/sql"
)
type Application struct {
*revel.Controller
}
func (c Application) Index() revel.Result {
if c.connected() != nil {
return c.Redirect(routes.Hotels.Index())
}
c.Flash.Error("Please log in first")
return c.Render()
}
func (c Application) connected() *models.User {
if c.ViewArgs["user"] != nil {
return c.ViewArgs["user"].(*models.User)
}
if username, ok := c.Session["user"]; ok {
return c.getUser(username.(string))
}
return nil
}
controllers/gorm.go
package controllers
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"booking/app/models"
"log"
)
type GormController struct {
*revel.Controller
Txn *gorm.DB
}
func InitDB() {
dbInfo, _ := revel.Config.String("db.info")
db, err := gorm.Open("mysql", dbInfo)
if err != nil {
log.Panicf("Failed gorm.Open: %v\n", err)
}
db.DB()
db.AutoMigrate(&models.Booking{})
db.AutoMigrate(&models.Hotel{})
db.AutoMigrate(&models.User{})
DB = db
}
func (c *GormController) SetDB() revel.Result {
c.Txn = DB
return nil
}
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
tree
enter image description here
maybe import miss?
I have this golang file:
package main
import (
"log"
"sync"
"github.com/jmoiron/sqlx"
)
var db *sqlx.DB
var once sync.Once
// GetDBConnection whatever
func GetDBConnection() {
once.Do(func() {
db, err := sqlx.Connect("postgres", "user=tom dbname=jerry password=myPassword sslmode=disable")
if err != nil {
log.Fatalln(err)
}
})
return db // <<< error here
}
I get this error:
Too many arguments to return
I am just trying to create a singleton pattern and return the db connection. I am not sure if what is returned from sqlx.Connect is type sqlx.DB, that might be the problem. Is there a quick way to determine the return type of sqlx.Connect()?
You've declared the function GetDBConnection() to return no arguments.
func GetDBConnection() {
You have to tell Go the type of the argument you intend to return:
func GetDBConnection() *sqlx.DB {
As for determining the type, I just went to look at the source code. You could also look at the documentation on godoc.org, which is auto-generated from publicly available Go packages.
I am faily new to Go and I am trying to create a structured application using guidance from Ben Johnson's webpage. Unfortunately, his example is not a complete working application.
His webpage is https://medium.com/#benbjohnson/standard-package-layout-7cdbc8391fc1
I have tried to use his methods and I keep getting "Undefined: db" error. It doesn't tell me what line is causing the error, just the file "MSSQL.go"
Could someone help with guidance to help me fix this error?
Edited code with accepted solution.
StatementPrinter.go
package statementprinter
type Statement struct {
CustomerId string
CustomerName string
}
type StatementService interface {
Statement(id string) (*Statement, error)
}
main.go
package main
import (
"fmt"
"log"
"github.com/ybenjolin/StatementPrinter"
"github.com/ybenjolin/StatementPrinter/mssql"
"database/sql"
_ "github.com/alexbrainman/odbc"
)
const DB_INFO = "Driver={SQL Server};Server=cdc-edb2;Database=CostarReports;Trusted_Connection=yes;"
var db *sql.DB
func init() {
var err error
db, err = sql.Open("odbc", DB_INFO)
if err != nil {
log.Fatal("Error opening database connection.\n", err.Error())
}
err = db.Ping()
if err != nil {
log.Fatal("Error pinging database server.\n", err.Error())
}
fmt.Println("Database connection established.")
}
func main () {
var err error
defer db.Close()
// Create services
// Changes required here. Was ss := &statementprinter.Stat..
ss := &mssql.StatementService{DB: db}
// Use service
var s *statementprinter.Statement
s, err = ss.Statement("101583")
if err != nil {
log.Fatal("Query failed:", err.Error())
}
fmt.Printf("Statement: %+v\n", s)
}
mssql.go
package mssql
import (
_ "github.com/alexbrainman/odbc"
"database/sql"
"github.com/ybenjolin/StatementPrinter"
)
// StatementService represents a MSSQL implementation of statemenetprinter.StatementService.
type StatementService struct {
DB *sql.DB
}
// Statement returns a statement for a given customer.
func (s *StatementService) Statement(customer string) (*statementprinter.Statement, error) {
var err error
var t statementprinter.Statement
// Changes required here. Was row := db.Query......
row := s.DB.QueryRow(`Select Customer, CustomerName From AccountsReceivable.rptfARStatementHeader(?)`, customer)
if row.Scan(&t.CustomerId, &t.CustomerName); err != nil {
return nil, err
}
return &t, nil
This seems like it's just a typo. It seems like the problematic line is in the method
func (s *StatementService) Statement(customer string)
in mssql.go,
row := db.QueryRow(`Select Customer, CustomerName From AccountsReceivable.rptfARStatementHeader(?)`, customer)
QueryRow is supposed to be a method of db, but db is not defined. However, in the struct
type StatementService struct {
DB *sql.DB
}
there's a *sql.DB instance. The method you're using has a *StatementService parameter, s. So, my guess is the intention would be to access the sql.DB field in s like so
func (s *StatementService) Statement(customer string) (*statementprinter.Statement, error) {
var err error
var t statementprinter.Statement
//CHANGED LINE:
row := s.DB.QueryRow(`Select Customer, CustomerName From AccountsReceivable.rptfARStatementHeader(?)`, customer)
if row.Scan(&t.CustomerId, &t.CustomerName); err != nil {
return nil, err
}
return &t, nil
Then, the method is called in main.go, and is passed a StatementService instance that contains a database:
ss := &statementprinter.StatementService{DB: db}
I believe you need to change this line to
ss := &mssql.StatementService{DB: db}
becuase that's the actual interface implementation. The line you have now treats the StatementService interface like a struct which will not compile.
The global db in main.go lives for the lifetime of the application. It's just a pointer which is copied around for use.
I understand the problem, as per the answer here, however, I could really use help or a more detailed code explanation of how it's overcome.
My situation is this: I used to have models and controllers separated, and in my models package I had a datastore.go file containing an interface of all the model functions:
package models
type DSDatabase interface {
CreateUser(ctx context.Context, username string, password []byte) (*datastore.Key, error)
// More model functions
}
type datastoreDB struct {
client *datastore.Client
}
var (
DB DSDatabase
_ DSDatabase = &datastoreDB{}
)
func init() {
// init datastore
}
This was all fine because the model functions were also located within the models package, so my functions in the controller package could freely call models.DB.CreateUser(ctx, "username", []byte("password")).
Now, I have decided to move all the above code to a datastore package, whereas the model for CreateUser is located in a user package. In other words, package user now contains both controller and model functions, for which the controller related functions rely on datastore package, while the DSDatabase interface rely on the user model functions.
I would really appreciate help figuring out how to overcome the import cycle while keeping the DSDatastore interface separate from all the other packages such as home and user.
in case the above is not clear enough, the above code has changed to:
package datastore
import (
"github.com/username/projectname/user"
)
type DSDatabase interface {
user.CreateUser(ctx context.Context, username string, passwoUserRegister(ctx context.Context, username string, password []byte) (*datastore.Key, error)
}
...
while in my user package I have this in a controller-related file:
package user
import (
"github.com/username/projectname/datastore"
)
func CreateUserPOST(w http.ResponseWriter, r *http.Request) {
// get formdata and such
datastore.DB.CreateUser(ctx, "username", []byte("password"))
}
and in another model-related file I have:
package user
import (
"github.com/username/projectname/datastore"
)
func (db *datastore.datastoreDB) CreateUser(ctx context.Context, username string) (*User, error) {
key := datastore.NameKey("User", username, nil)
var user User
err := db.client.Get(ctx, key, &user)
if err != nil {
return nil, err
}
return &user, nil
}
Which of course results in an import cycle, that I sadly can't figure out how to overcome..
First things first, you cannot define a method, in pacakge A, on a type declared in package B.
So this...
package user
import (
"github.com/username/projectname/datastore"
)
func (db *datastore.datastoreDB) CreateUser(ctx context.Context, username string) (*User, error) {
key := datastore.NameKey("User", username, nil)
var user User
err := db.client.Get(ctx, key, &user)
if err != nil {
return nil, err
}
return &user, nil
}
...should not even compile.
This here...
package datastore
import (
"github.com/username/projectname/user"
)
type DSDatabase interface {
user.CreateUser(ctx context.Context, username string, passwoUserRegister(ctx context.Context, username string, password []byte) (*datastore.Key, error)
}
...this is also invalid Go code.
As to your question... one thing you could do is to define the Datastore interface inside the user package and have the implementation live in another package, this lends itself nicely for when you need different implementations of one interface. If you do this your user package does not need to know about the datastore package anymore, the datastore package still has to know about the user package though, which is a OK.
An example:
package user
import (
"context"
)
type DSDatabase interface {
CreateUser(ctx context.Context, username string, password []byte) (*User, error)
// ...
}
// This can be set by the package that implements the interface
// or by any other package that imports the user package and
// a package that defines an implementation of the interface.
var DB DSDatabase
type User struct {
// ...
}
func CreateUserPOST(w http.ResponseWriter, r *http.Request) {
// get formdata and such
DB.CreateUser(ctx, "username", []byte("password"))
}
The package with the implementation of the interface:
package datastore
import (
"context"
"github.com/username/projectname/user"
)
// DB implements the user.DSDatabase interface.
type DB struct { /* ... */ }
func (db *DB) CreateUser(ctx context.Context, username string) (*user.User, error) {
key := datastore.NameKey("User", username, nil)
var user user.User
err := db.client.Get(ctx, key, &user)
if err != nil {
return nil, err
}
return &user, nil
}
func init() {
// make sure to initialize the user.DB variable that
// is accessed by the CreateUserPOST func or else you'll
// get nil reference panic.
user.DB = &DB{}
}
I have a simple golang routine that uses database/sql to open a connection to my Postgres DB and does some stuff
package main
import (
"fmt"
"database/sql"
_ "github.com/lib/pq"
"log"
)
const (
DB_USER = "my_user"
DB_NAME = "my_postgres_db"
)
// The return type here is wrong - what should it be?
func establish_db_connection() sql.DB {
dbinfo := fmt.Sprintf(
"user=%s password=%s dbname=%s sslmode=disable",
DB_USER, nil, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
if err != nil { log.Fatal(err) }
return db
}
func main() {
// Get a connection to the DB
db := establish_db_connection()
// Do other stuff
// ...
// ...
}
I'm having trouble writing the signature for the establish_db_connection function -
func establish_db_connection() sql.DB {
The documentation suggests it returns a sql.DB instance. So should this not be the return type?
I'm super new to golang, so just figuring most of this out for the first time.
Thanks!
Open returns a *sql.DB, a pointer to a sql.Db. Change the function signature to also return a *sql.DB:
func establish_db_connection() *sql.DB {