IOT>> pg>> 返回
项目作者: go-pg

项目描述 :
Golang ORM with focus on PostgreSQL features and performance
高级语言: Go
项目地址: git://github.com/go-pg/pg.git
创建时间: 2013-04-24T12:31:41Z
项目社区:https://github.com/go-pg/pg

开源协议:BSD 2-Clause "Simplified" License

下载


PostgreSQL client and ORM for Golang

Maintenance mode

go-pg is in a maintenance mode and only critical issues are addressed. New development happens in
Bun repo which offers similar functionality
but works with PostgreSQL, MySQL, MariaDB, and SQLite.

Golang ORM


Go
PkgGoDev
Documentation
Chat

Tutorials

Ecosystem

Features

Installation

go-pg supports 2 last Go versions and requires a Go version with
modules support. So make sure to initialize a Go
module:

  1. go mod init github.com/my/repo

And then install go-pg (note v10 in the import; omitting it is a popular mistake):

  1. go get github.com/go-pg/pg/v10

Quickstart

  1. package pg_test
  2. import (
  3. "fmt"
  4. "github.com/go-pg/pg/v10"
  5. "github.com/go-pg/pg/v10/orm"
  6. )
  7. type User struct {
  8. Id int64
  9. Name string
  10. Emails []string
  11. }
  12. func (u User) String() string {
  13. return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
  14. }
  15. type Story struct {
  16. Id int64
  17. Title string
  18. AuthorId int64
  19. Author *User `pg:"rel:has-one"`
  20. }
  21. func (s Story) String() string {
  22. return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
  23. }
  24. func ExampleDB_Model() {
  25. db := pg.Connect(&pg.Options{
  26. User: "postgres",
  27. })
  28. defer db.Close()
  29. err := createSchema(db)
  30. if err != nil {
  31. panic(err)
  32. }
  33. user1 := &User{
  34. Name: "admin",
  35. Emails: []string{"admin1@admin", "admin2@admin"},
  36. }
  37. _, err = db.Model(user1).Insert()
  38. if err != nil {
  39. panic(err)
  40. }
  41. _, err = db.Model(&User{
  42. Name: "root",
  43. Emails: []string{"root1@root", "root2@root"},
  44. }).Insert()
  45. if err != nil {
  46. panic(err)
  47. }
  48. story1 := &Story{
  49. Title: "Cool story",
  50. AuthorId: user1.Id,
  51. }
  52. _, err = db.Model(story1).Insert()
  53. if err != nil {
  54. panic(err)
  55. }
  56. // Select user by primary key.
  57. user := &User{Id: user1.Id}
  58. err = db.Model(user).WherePK().Select()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Select all users.
  63. var users []User
  64. err = db.Model(&users).Select()
  65. if err != nil {
  66. panic(err)
  67. }
  68. // Select story and associated author in one query.
  69. story := new(Story)
  70. err = db.Model(story).
  71. Relation("Author").
  72. Where("story.id = ?", story1.Id).
  73. Select()
  74. if err != nil {
  75. panic(err)
  76. }
  77. fmt.Println(user)
  78. fmt.Println(users)
  79. fmt.Println(story)
  80. // Output: User<1 admin [admin1@admin admin2@admin]>
  81. // [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
  82. // Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
  83. }
  84. // createSchema creates database schema for User and Story models.
  85. func createSchema(db *pg.DB) error {
  86. models := []interface{}{
  87. (*User)(nil),
  88. (*Story)(nil),
  89. }
  90. for _, model := range models {
  91. err := db.Model(model).CreateTable(&orm.CreateTableOptions{
  92. Temp: true,
  93. })
  94. if err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }

See also