Guest User

Untitled

a guest
Jan 27th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. _ "github.com/lib/pq"
  8. )
  9.  
  10. type Post struct {
  11. Id int
  12. Content string
  13. Author string
  14. Comments []Comment
  15. }
  16.  
  17. type Comment struct {
  18. Id int
  19. Content string
  20. Author string
  21. Post *Post
  22. }
  23.  
  24. var Db *sql.DB
  25.  
  26. // connect to the Db
  27. func init() {
  28. var err error
  29. //Db, err = sql.Open("postgres", "user=gwp dbname=gwp password=gwp sslmode=disable")
  30. Db, err = sql.Open("postgres", "user=gwp dbname=gwp sslmode=disable")
  31. if err != nil {
  32. panic(err)
  33. }
  34. }
  35.  
  36. func (comment *Comment) Create() (err error) {
  37. if comment.Post == nil {
  38. err = errors.New("You need to specify the post that the comment belongs to.")
  39. return
  40. }
  41. err = Db.QueryRow("insert into comments (content, author, post_id) values ($1, $2, $3) returning id", comment.Content, comment.Author, comment.Post.Id).Scan(&comment.Id)
  42. return
  43. }
  44.  
  45. // Get a single post
  46. func GetPost(id int) (post Post, err error) {
  47. post = Post{}
  48. post.Comments = []Comment{}
  49. err = Db.QueryRow("select id, content, author from posts where id = $1", id).Scan(&post.Id, &post.Content, &post.Author)
  50.  
  51. rows, err := Db.Query("select id, content, author from comments where post_id = $1", id)
  52. if err != nil {
  53. return
  54. }
  55. for rows.Next() {
  56. comment := Comment{Post: &post}
  57. err = rows.Scan(&comment.Id, &comment.Content, &comment.Author)
  58. if err != nil {
  59. return
  60. }
  61. post.Comments = append(post.Comments, comment)
  62. }
  63. rows.Close()
  64. return
  65. }
  66.  
  67. // Create a new post
  68. func (post *Post) Create() (err error) {
  69. err = Db.QueryRow("insert into posts (content, author) values ($1, $2) returning id", post.Content, post.Author).Scan(&post.Id)
  70. return
  71. }
  72.  
  73. func main() {
  74. post := Post{Content: "Hello World!", Author: "Sau Sheong"}
  75. post.Create()
  76.  
  77. // Add a comment
  78. comment := Comment{Content: "Good post!", Author: "Joe", Post: &post}
  79. comment.Create()
  80. readPost, _ := GetPost(post.Id)
  81.  
  82. fmt.Println(readPost) // {1 Hello World! Sau Sheong [{1 Good post! Joe 0xc20802a1c0}]}
  83. fmt.Println(readPost.Comments) // [{1 Good post! Joe 0xc20802a1c0}]
  84. fmt.Println(readPost.Comments[0].Post) // &{1 Hello World! Sau Sheong [{1 Good post! Joe 0xc20802a1c0}]}
  85. }
  86.  
  87. /usr/local/Cellar/go/1.7.4_2/libexec/bin/go run /Users/kaiyin/golang/src/github.com/sausheong/gwp/Chapter_6_Storing_Data/sql_store2/store.go
  88. panic: runtime error: invalid memory address or nil pointer dereference
  89. [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5fa9a]
  90.  
  91. goroutine 1 [running]:
  92. panic(0x1d62c0, 0xc42000c0e0)
  93. /usr/local/Cellar/go/1.7.4_2/libexec/src/runtime/panic.go:500 +0x1a1
  94. database/sql.(*DB).conn(0x0, 0x1, 0x22, 0x1c, 0x0)
  95. /usr/local/Cellar/go/1.7.4_2/libexec/src/database/sql/sql.go:781 +0x3a
  96. database/sql.(*DB).query(0x0, 0x216def, 0x40, 0xc42004fd80, 0x2, 0x2, 0x1, 0x10, 0xc42000cba0, 0x0)
  97. /usr/local/Cellar/go/1.7.4_2/libexec/src/database/sql/sql.go:1074 +0x3b
  98. database/sql.(*DB).Query(0x0, 0x216def, 0x40, 0xc42004fd80, 0x2, 0x2, 0x1, 0xc42000cba0, 0xc42004fd28)
  99. /usr/local/Cellar/go/1.7.4_2/libexec/src/database/sql/sql.go:1062 +0x90
  100. database/sql.(*DB).QueryRow(0x0, 0x216def, 0x40, 0xc42004fd80, 0x2, 0x2, 0x11200b7b18)
  101. /usr/local/Cellar/go/1.7.4_2/libexec/src/database/sql/sql.go:1143 +0x70
  102. main.(*Post).Create(0xc420018380, 0xc420018380, 0xc42004fe20)
  103. /Users/kaiyin/golang/src/github.com/sausheong/gwp/Chapter_6_Storing_Data/sql_store2/store.go:69 +0xfe
  104. main.main()
  105. /Users/kaiyin/golang/src/github.com/sausheong/gwp/Chapter_6_Storing_Data/sql_store2/store.go:75 +0xd7
  106. exit status 2
  107.  
  108. Process finished with exit code 1
Add Comment
Please, Sign In to add comment