eddycjy / blog

煎鱼的博客,有点忙,传送门:https://eddycjy.com
3.05k stars 431 forks source link

posts/go/gin/2018-02-11-api-01/ #65

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

「连载二」Gin搭建Blog API's (一)

https://eddycjy.com/posts/go/gin/2018-02-11-api-01/

littlestar1998 commented 4 years ago

写完 setting.go的当前的目录结构应该是 setting/setting.go

jmy10241024 commented 4 years ago
replace (
        github.com/EDDYCJY/go-gin-example/pkg/setting => ~/go-application/go-gin-example/pkg/setting
)

go version 1.13.5 中, 需要将replace路径指定为相对路径, 否则报路径错误, 这是我的路径


replace (
    gin-blog/conf => ./conf
)
FicowShen commented 4 years ago

go 1.14 里,如果没有在mod文件中replace路径,好像也没什么问题。不知道是不是因为我用了VSCode。 感谢作者~

liyixiang19 commented 4 years ago

go1.14 goland同上,没有添加replace也没问题

23494330 commented 4 years ago

数据库部分的代码是写到哪里的呢

FicowShen commented 4 years ago

@23494330 应该是自己用SQL语句去数据库执行然后创建的,虽然这里用了ORM,但是建数据库、建表都没有相应的代码呢

23494330 commented 4 years ago

要是代码加点注释就更好了,很多都不知道什么意思

23494330 commented 4 years ago

忽略了go-ini的文档,读了后了解点了,就是从来没操作过数据库,现在就卡在这儿了

okcomputerb commented 4 years ago

实践下来,replace那里有误解,用下面这句来代替更方便

 replace github.com/EDDYCJY/go-gin-example => ../go-gin-example 

StackOverflow对这个问题的参考链接

lvpengc commented 3 years ago

不错!

zhonglangjp commented 3 years ago
        ReadTimeout = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second
    WriteTimeout = time.Duration(sec.Key("WRITE_TIMEOUT").MustInt(60)) * time.Second
bigdllmask1333 commented 3 years ago

真是太棒了,我对着复制粘贴全部能执行跑起来了,接下来得慢慢理解。不习惯基础语法,看了几遍还是啥都不会,这个给力直接都能CURD了,不多说,牛逼

ljlong0908 commented 3 years ago

很容易理解,适合新手学习。

appleboy commented 3 years ago

更多 Gin 的範例可以來這邊看: https://github.com/gin-gonic/examples

bigdllmask1333 commented 3 years ago

再次过来留言,基础语法会了,接下来就是学习接收这种风格。然后再发散思维

PeterTuT commented 3 years ago

go: errors parsing go.mod: D:/GO-demo/src/go-gin-example/go.mod:26:3: replacement module without version must be directory path (rooted or starting with ./ or ../) D:/GO-demo/src/go-gin-example/go.mod:27:3: replacement module without version must be directory path (rooted or starting with ./ or ../) D:/GO-demo/src/go-gin-example/go.mod:28:3: replacement module without version must be directory path (rooted or starting with ./ or ../) D:/GO-demo/src/go-gin-example/go.mod:29:3: replacement module without version must be directory path (rooted or starting with ./ or ../) D:/GO-demo/src/go-gin-example/go.mod:30:3: replacement module without version must be directory path (rooted or starting with ./ or ../)

PeterTuT commented 3 years ago

要改成以下

replace ( github.com/EDDYCJY/go-gin-example/pkg/setting => ./conf/pkg/setting github.com/EDDYCJY/go-gin-example/conf => ./conf/pkg/conf github.com/EDDYCJY/go-gin-example/middleware => ./conf/middleware github.com/EDDYCJY/go-gin-example/models => ./conf/models github.com/EDDYCJY/go-gin-example/routers => ./conf/routers )

Wilsonoonn commented 2 years ago

这个主题是啥呀,看着很舒服

EnzoLwb commented 2 years ago

models.go 中的 _ "github.com/jinzhu/gorm/dialects/mysql" 不要忘记了,不然会提示 sql: unknown driver "mysql" (forgotten import?)

firefake commented 2 years ago

unkown/com这个包是干做什么的? 只是简单的转换数据类型吗? 我好奇为啥不用标准库中的转换方式?

edsionxuanxuan commented 2 years ago

数据库那块最新的代码:用的是新版gorm及mysql,go版本1.15 package models

import ( "fmt" "log"

"gindemo/pkg/setting"

"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"

)

var db *gorm.DB

type Model struct { ID int gorm:"primary_key" json:"id" CreatedOn int json:"created_on" ModifiedOn int json:"modified_on" }

func init() { var ( err error dbName, user, password, host string ) sec, err := setting.Cfg.GetSection("database") if err != nil { log.Fatalf("Fail to get section 'database' : %v", err) }

// dbType = sec.Key("TYPE").String()
dbName = sec.Key("NAME").String()
user = sec.Key("USER").String()
password = sec.Key("PASSWORD").String()
host = sec.Key("HOST").String()

db, err = gorm.Open(
    mysql.Open(fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
        user,
        password,
        host,
        dbName)), &gorm.Config{
        NamingStrategy: schema.NamingStrategy{
            TablePrefix:   "blog_", // 表名前缀,`Tag` 的表名应该是 `blog_tag`
            SingularTable: true,    // 使用单数表名,启用该选项,此时,`Tag` 的表名应该是 `blog_tag`
        },
    })
if err != nil {
    log.Println(err)
}

sqlDB, err := db.DB()
if err != nil {
    log.Println(err)
}
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(100)

}

// func CloseDB() { // sqlDB, err := db.DB() // defer sqlDB.Close() // if err != nil { // fmt.Println(err) // } // }

canwoh commented 2 years ago

log.Fatal(2, "Fail to get section 'database': %v", err) // models.go 中 现阶段很好奇参数中 ‘2’ 起到了什么作用?(尚未看完整个项目代码)

TimidHaunter commented 2 years ago

go.mod里面的replace{}怎么写,在引用的时候import都是报红报错。上面评论区的方法都用过了,我是go1.18。

appleboy commented 2 years ago
module example

go 1.18

require github.com/golang-queue/queue v0.0.7

replace github.com/golang-queue/queue => ../../

See https://github.com/golang-queue/queue/blob/140c51202d551d21038c74a1c3970946e09d5fbf/_example/example01/go.mod#L1-L12

TimidHaunter commented 2 years ago

pkg/setting这个文件夹需要go mod init嘛?我只把go-gin-example初始化了。

huangpengju commented 1 year ago

看了一些评论,我也说一下:初始化项目数据库

huangpengju commented 1 year ago

在本篇文章中,作者暂时没有介绍 "AutoMigrate"自动迁移,所以大家需要手动的创建数据库“blog”,以及库中的三个表“blog_tag”“blog_article”“blog_auth”。创建数据库参考命令:CREATE DATABASE blog CHARACTER SET utf8 COLLATE utf8_general_ci;