Go server returning exist status 2 immediately after starting server

Viewed 43

The config/app.go folder

config/app.go

package config

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

var (
    db *gorm.DB
)

func Connect() {
    d, err := gorm.Open("mysql", "nicholasovunda;password/simplerest?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        panic(err)
    }
    db = d
}

func GetDB() *gorm.DB {
    return db
}

models/book.go

package models

import (
    "github.com/jinzhu/gorm"
    "github.com/nicholasovunda/go-bookstore/pkg/config"
)

var db *gorm.DB

type Book struct {
    gorm.Model
    Name        string `gorm:" " json:"name"`
    Author      string `json:"author"`
    Publication string `json:"publication"`
}

func init() {
    config.Connect()
    db = config.GetDB()
    db.AutoMigrate(&Book{})
}

func (b *Book) CreateBook() *Book {
    db.NewRecord(b)
    db.Create(&b)
    return b
}
func GetAllBooks() []Book {
    var Books []Book
    db.Find(&Books)
    return Books
}

func GetBookById(Id int64) (*Book, *gorm.DB) {
    var getBook Book
    db := db.Where("ID=?", Id).Find(&getBook)
    return &getBook, db
}

func DeleteBook(ID int64) Book {
    var book Book
    db.Where("ID=?", ID).Delete(book)
    return book
}

The error code when i run the file

goroutine 1 [running]:
github.com/nicholasovunda/go-bookstore/pkg/config.Connect(...)
/Users/nicholas/development/go-bookstore/pkg/config/app.go:15
github.com/nicholasovunda/go-bookstore/pkg/models.init.0()
/Users/nicholas/development/go-bookstore/pkg/models/book.go:18+0xe5
exit status 2
1 Answers

The DSN is in the wrong format.

"nicholasovunda;password/simplerest?charset=utf8&parseTime=True&loc=Local"

should be

"nicholasovunda:password@YOURDBHOSTNAME/simplerest?charset=utf8&parseTime=True&loc=Local"

(it's missing the hostname and the separator between user and password should be a ":", not a ";")

Related