Hello I am trying to make a connection to postgres using go, but I have the following error:
on this line :
conn.pool, err := sql.Open("postgres", uri)
err:
expected identifier on left side of :=syntax
I'm new to go and I don't know why and how to resolve this error, if someone can help me where I'm going wrong.
package database
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
)
type Db struct {
pool *sql.DB
}
type Connection interface {
Close()
DB() *sql.DB
}
func NewPostgreSQLsql() (Connection, error) {
var conn Db
uri := getURI()
conn.pool, err := sql.Open("postgres", uri)
if err != nil {
log.Fatal(err.Error())
return nil, err
}
if err := conn.Ping(); err != nil {
log.Fatal(err.Error())
return nil, err
}
return &conn, nil
}
func (c *db) DB() *sql.DB {
return c.pool
}
func (c *Db) Close() {
c.pool.Close()
}
func getURI() string {
dbPort, err := strconv.Atoi(os.Getenv("DB_PORT"))
if err != nil {
log.Println("error on load db port from env:", err.Error())
dbPort = 5432
}
return fmt.Sprintf(os.Getenv("DB_HOST"),
dbPort,
os.Getenv("DB_USER"),
os.Getenv("DB_NAME"),
os.Getenv("DB_PASSWORD"))
}
you cannot directly initialize and assign to conn.pool with :=
and you should initialize conn object
Try this
conn := Db{}
uri := getURI()
pool, err := sql.Open("postgres", uri)
conn.pool = pool
or
uri := getURI()
pool, err := sql.Open("postgres", uri)
conn := Db{pool}
Related
I use Gorm for Golang like this code,
func Connect() (*gorm.DB, error) {
var (
err error
db *gorm.DB
)
dsn := "sqlserver://User:12345#127.0.0.1:1433?database=gorm"
db, err = gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
err = db.Debug().AutoMigrate(&models.CompanyName{}, &models.CarModel{}, &models.CreateYear{}, &models.Diversity{})
if err != nil {
return nil, err
}
return db, nil
}
and in main.go
func main() {
db, err := Connect()
if err != nil {
panic("database connection failed")
}
ech := echo.New()
}
Ok in the first time when we run code, gorm create tables in database,but in the second time I got an error thet
mssql: There is already an object named 'table_names' in the database.
Do I have to delete db.Debug().AutoMigrate)(?
I am trying to create an API with Go, gorm and gin. However, using a direct SQL query did work, but it became too cumbersome and I decided to leverage gorm which is fantastic. However after my implementation, I am unable to get the project up again and keep getting error
panic: runtime error: invalid memory address or nil pointer
dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0xb0
pc=0x137b606]
I have pasted my code with packages here. I would be glad if someone could point me to the issue.
PostgresDoa.go
package postgres
import (
"fmt"
"log"
"net/http"
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
type Sever struct {
DB *gorm.DB
Router *gin.Engine
}
func (server *Sever) Initialize(Dbdriver, DbUser, DbPassword, DbPort, DbHost, DbName string) {
var err error
if Dbdriver == "mysql" {
DBURL := fmt.Sprintf("%s:%s#tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", DbUser, DbPassword, DbHost, DbPort, DbName)
server.DB, err = gorm.Open(Dbdriver, DBURL)
if err != nil {
fmt.Printf("Cannot connect to %s database", Dbdriver)
log.Fatal("This is the error:", err)
} else {
fmt.Printf("We are connected to the %s database", Dbdriver)
}
}
if Dbdriver == "postgres" {
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", DbHost, DbPort, DbUser, DbName, DbPassword)
server.DB, err = gorm.Open(Dbdriver, DBURL)
if err != nil {
fmt.Printf("Cannot connect to %s database", Dbdriver)
log.Fatal("This is the error:", err)
} else {
fmt.Printf("We are connected to the %s database", Dbdriver)
}
}
server.DB.Debug().AutoMigrate(&users.User{}) //database migration
server.Router = gin.Default()
}
func (server *Sever) Run(addr string) {
fmt.Println("Listening to port 8080")
log.Fatal(http.ListenAndServe(addr, server.Router))
}
UserDto.go
package users
import (
"strings"
"github.com/AdieOlami/bookstore_users-api/domain/base"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
type User struct {
base.Base
UserID uuid.UUID `gorm:"type:uuid;column:userId;not null;" json:"userId"`
FirstName string `gorm:"size:255;not null;unique;column:firstName" json:"firstName"`
LastName string `gorm:"size:255;not null;unique;column:lastName" json:"lastName"`
Email string `gorm:"size:100;not null;unique;column:email" json:"email"`
}
func (user *User) Validate() *errors.Error {
user.Email = strings.TrimSpace(strings.ToLower(user.Email))
if user.Email == "" {
return errors.NewBadRequestError("invalid email address")
}
return nil
}
func (user *User) SaveUser(db *gorm.DB) *errors.Error {
var err error
err = db.Debug().Create(&user).Error
if err != nil {
return errors.NewInteralServerError(err.Error())
}
return nil
}
UserService.go
package services
import (
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
)
func (server *Server) CreateUser(user users.User) (*users.User, *errors.Error) {
if err := user.Validate(); err != nil {
return nil, err
}
if err := user.SaveUser(server.DB); err != nil {
return nil, err
}
return &user, nil
}
UserController.go
package users
import (
"net/http"
"strconv"
"github.com/AdieOlami/bookstore_users-api/domain/users"
"github.com/AdieOlami/bookstore_users-api/services"
"github.com/AdieOlami/bookstore_users-api/utils/errors"
"github.com/gin-gonic/gin"
)
var (
server = services.Server{}
)
// CREATE USER
func CreateUser(c *gin.Context) {
var user users.User
if err := c.ShouldBindJSON(&user); err != nil {
// TODO: hnadle json error return bad request
err := errors.NewBadRequestError("invalid json body")
c.JSON(err.Status, err)
// fmt.Println(err.Error())
return
}
result, saveErr := server.CreateUser(user)
if saveErr != nil {
// TODO: handle user createing error
c.JSON(saveErr.Status, saveErr)
return
}
c.JSON(http.StatusCreated, result)
}
Routes.go
package app
import (
"github.com/AdieOlami/bookstore_users-api/controllers/users"
"github.com/AdieOlami/bookstore_users-api/database/postgres"
)
var (
server = postgres.Sever{}
)
func initializeRoutes() {
server.Router.POST("/users", users.CreateUser)
}
Application.go
package app
import (
"os"
"github.com/AdieOlami/bookstore_users-api/seed"
_ "github.com/jinzhu/gorm/dialects/mysql" //mysql database driver
_ "github.com/jinzhu/gorm/dialects/postgres" //postgres database driver
)
func StartApplication() {
server.Initialize(os.Getenv("DB_DRIVER"), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_PORT"), os.Getenv("DB_HOST"), os.Getenv("DB_NAME"))
seed.Load(server.DB)
server.Run(":8088")
initializeRoutes()
}
in my Main.go
func main() {
app.StartApplication()
}
As far as I can understand from your code. You declared below function in Services package whereas Server object is declared in the Postgres package. This causes dereferencing of pointer server *Server to an invalid address. you have to declare this function in posgres package.
func (server *Server) CreateUser(user users.User) (*users.User, *errors.Error) {
if err := user.Validate(); err != nil {
return nil, err
}
if err := user.SaveUser(server.DB); err != nil {
return nil, err
}
return &user, nil
}
I am working on a Go project which use gRPC as server and Voltdb as DB. I have an issue when sending many DB read/write requests to gRPC server. Request has been handled but it does not release DB connection after finishing operator. So when connection in pool run out I get this error:
Rejected connection from /127.0.0.1:63015 because the connection limit of 9940 has been reached
proto file
syntax = "proto3";
package grpc;
import "google/protobuf/struct.proto";
option java_multiple_files = true;
option java_package = "com.io.test";
service TestService {
rpc updateUser (UserRequest) returns (Status) {};
}
message UserRequest {
string userId = 1;
string name = 2;
}
message Status {
string code = 1;
}
Grpc implement in Go
package service
import (
"context"
"log"
api "testgrpc/pkg/api/grpc"
"testgrpc/pkg/db"
"testgrpc/pkg/model"
)
var voltDbClient *db.VoltDBClient
func init() {
voltDbClient = db.NewVoltDBClient()
}
type GrpcService struct {
}
func NewGrpcService() *GrpcService {
return &GrpcService{}
}
func (sv *GrpcService) UpdateUser(ctx context.Context, req *api.UserRequest) (*api.Status, error) {
user, err := voltDbClient.FindByID(req.GetUserId())
if user == nil || err != nil {
log.Print("error while reading")
} else {
log.Println(user.(*model.User).UserId)
log.Println(user.(*model.User).Name)
}
err = voltDbClient.Put(req.GetUserId(), req.GetName())
if err != nil {
log.Print("error while writing")
}
return &api.Status{Code: "0"}, nil
}
DB client
package db
import (
"database/sql"
"log"
"testgrpc/pkg/model"
_ "github.com/VoltDB/voltdb-client-go/voltdbclient"
)
var Db *sql.DB
func init() {
if Db == nil {
var err error
Db, err = sql.Open("voltdb", "localhost:21212")
if err != nil {
log.Fatal(err)
}
//limit max connection to reproduce issue
Db.SetMaxOpenConns(20)
}
}
type VoltDBClient struct {
}
func NewVoltDBClient() *VoltDBClient {
return &VoltDBClient{}
}
//Put add or update record
func (dbClient *VoltDBClient) Put(key interface{}, value interface{}) error {
res, err := Db.Exec("GROUPINFO.upsert", key, value)
if err != nil {
log.Println(err)
return err
}
rowCnt, err := res.RowsAffected()
if err != nil {
log.Println(err)
return err
}
log.Printf("affected = %d\n", rowCnt)
return nil
}
//FindByID find a record by ID
func (dbClient *VoltDBClient) FindByID(key interface{}) (interface{}, error) {
rows, err := Db.Query("GROUPSELECT", key)
if err != nil {
log.Println(err)
return nil, err
}
for rows.Next() {
user := model.User{UserId: "", Name: ""}
err := rows.Scan(&user.UserId, &user.Name)
if err != nil {
log.Println(err)
return nil, err
}
return &user, nil
}
return nil, nil
}
Note: I have tried run project without grpc, read/write operators work well.
I am using Mongo-driver with gin framework. I have written code to connect mongodb in DB package and if I write query inside db/connect.go, it works but when I use same dbcon in other package it doesn't.
db/connect.go:
var dbcon *mongo.Database
func ConfigDB() (*mongo.Database) {
ctx := context.Background()
client, err := mongo.Connect(
ctx,
options.Client().ApplyURI("mongodb://localhost:27017/todo"),
)
if err != nil {
log.Fatal(err)
}
dbcon = client.Database("todo")
}
if I use the code below in same db/connect.go, then it works but when I use the same code in handler/task.go, then it won't.
func CreateTask() () {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := dbcon.Collection("ttest").InsertOne(ctx, bson.D{
{"task", "test4"},
{"createdAt", "test"},
{"modifiedAt","test3"},
})
if err != nil {
fmt.Println( err))
}
}
I have to implement a mongo-driver in my project, but due to above issue I am facing problem to implement.
You'll have to import to import the db/connect.go file into the handler/task.go. This is not working because they are in different packages.
In my opinion you could refactor your code like this
func ConfigDB() (*mongo.Database) {
ctx := context.Background()
client, err := mongo.Connect(
ctx,
options.Client().ApplyURI("mongodb://localhost:27017/todo"),
)
if err != nil {
log.Fatal(err)
}
return client.Database("todo")
}
import (
"db/connect"
)
func CreateTask() () {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := ConfigDB().Collection("test").InsertOne(ctx, bson.D{
{"task", "test4"},
{"createdAt", "test"},
{"modifiedAt","test3"},
})
if err != nil {
fmt.Println( err))
}
}
Here I post a complete working example.
I catch the mongo session connection in a global variable. So that, anywhere in the project you can access the active connection.
package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// SESSION ensure global mongodb connection
var SESSION *mgo.Session
func init() {
// mongodb manual connection using host ip. Replace your host IP address there
session, err := mgo.Dial("172.17.0.2")
// session, err := mgo.Dial("<HostIP>")
Must(err)
fmt.Println(err)
SESSION = session
}
func main() {
port := os.Getenv("PORT")
gin.SetMode(gin.ReleaseMode)
// gin.SetMode(gin.DebugMode)
r := gin.Default()
r.Use(mapMongo)
if port == "" {
port = "8000"
}
r.POST("/api/v1/task", CreateTask)
http.ListenAndServe(":"+port, r)
}
// close connection
func mapMongo(c *gin.Context) {
s := SESSION.Clone()
defer s.Close()
c.Set("mongo", s.DB("mongotask"))
c.Next()
}
// Must to catch the mongo panic issues
func Must(err error) {
if err != nil {
panic(err.Error())
}
}
// NewTask Struct/model
type NewTask struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Task string
}
// Mongo bson generate New unique Id each request
func (self *NewTask) Init() {
self.Id = bson.NewObjectId()
}
const (
// CollectionTask is the collection name
CollectionTask = "taskCollection"
)
// CreateTask to create new Task message
func CreateTask(c *gin.Context) {
var newTask NewTask
err := c.BindJSON(&newTask)
if err != nil {
c.Error(err)
return
}
mongodb := c.MustGet("mongo").(*mgo.Database)
con := mongodb.C(CollectionTask)
// fmt.Println(newTask)
con.Insert(newTask)
if err != nil {
c.Error(err)
}
}
I would like to import package and create new struct in main() func.
// main.go
import "testapp/app"
a := app.GetApp()
db, err := a.ConnectDatabase()
if err != nil {
panic(err.Error())
}
// testapp/app.go
func (a *App) ConnectDatabase() {
db, err := sql.Open()
if err != nil {
panic(err.Error())
}
a.db = db
}
I've got error:
app.ConnectDatabase() used as value
How can I fix that?
you might want to solve this like:
// main.go
import "testapp/app"
func main(){
a := app.GetApp()
err := a.ConnectDatabase()
if err != nil {
panic(err.Error())
}
a.db. //interesting db code here
}
// testapp/app.go
func (a *App) ConnectDatabase() error{
db, err := sql.Open()
if err != nil {
return err
}
a.db = db
return nil
}