英文:
Go server returning exist status 2 immediately after starting server
问题
config/app.go 文件
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
}
运行文件时的错误代码
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
英文:
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
得分: 1
DSN的格式不正确。
"nicholasovunda;password/simplerest?charset=utf8&parseTime=True&loc=Local"
应该是
"nicholasovunda:password@YOURDBHOSTNAME/simplerest?charset=utf8&parseTime=True&loc=Local"
(缺少主机名,用户名和密码之间的分隔符应该是“:”,而不是“;”)
英文:
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 ";")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论