type Like struct { ID int`gorm:"column:f_id;primary_key"` Ip string`gorm:"type:varchar(20);not null;index:ip_idx"` Ua string`gorm:"type:varchar(256);not null;"` Title string`gorm:"type:varchar(128);not null;index:title_idx"` Hash uint64`gorm:"unique_index:hash_idx;"` CreatedAt time.Time }
连接
mysql
import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) var db *gorm.DB funcinit() { var err error db, err = gorm.Open("mysql", "<user>:<password>/<database>?charset=utf8&parseTime=True&loc=Local") if err != nil { panic(err) } }
方法 Delete 接受参数后,会自动根据传进去的值进行查找,然后删除。比如此处,我们指定了 Account 的 ID 字段,那么就会删除 ID 字段值与我们所赋值相同的记录;如果您只对 Name 字段赋值,那么 xorm 就会去查找 Name 字段值匹配的记录。如果多个字段同时赋值,则是多个条件同时满足的记录才会被删除。
删除操作针对的对象没有限制,凡是按照条件查找到的,都会被删除(单个与批量删除)。
_, err := x.Delete(&Account{Id: id})
if _, err := session.Exec("delete from userinfo where username = ?", user2.Username); err != nil { return err }
// rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, to distinguish character values from integer values.
initialize new module in current directory(在当前目录初始化mod)
tidy
add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
vendor
make vendored copy of dependencies(将依赖复制到vendor下)
verify
verify dependencies have expected content (验证依赖是否正确)
why
explain why packages or modules are needed(解释为什么需要依赖)
创建一个新项目
在 GOPATH 目录之外新建一个目录,并使用 go mod init 初始化生成go.mod 文件 go.mod文件一旦创建后,它的内容将会被go toolchain全面掌控。go toolchain会在各类命令执行时,比如go get、go build、go mod等修改和维护go.mod文件。
项目名 hello
go mod init hello
执行 go run server.go 运行代码会发现 go mod 会自动查找依赖自动下载: go module 安装 package 的原則是先拉最新的 release tag,若无tag则拉最新的commit,详见 Modules官方介绍。 go 会自动生成一个 go.sum 文件来记录 dependency tree:
goland
摘录至
在 GoLand 2019.1.3 中使用 Go Modules 需要进行两个设置:
Preferences -> Go -> Go Modules (vgo),勾选 Enable Go Modules (vgo) integration 以启用 Go Modules,并在 Proxy 输入框中输入 https://goproxy.io。如图所示:
Preferences -> Go -> GOPATH,勾选上 Index entire GOPATH 以索引整个 GOPATH,不然无法导入包。如图所示: