启动服务器后,Go服务器立即返回存在状态2。

huangapple go评论83阅读模式
英文:

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 ";")

huangapple
  • 本文由 发表于 2022年3月31日 15:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71688349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定