-
Notifications
You must be signed in to change notification settings - Fork 22
/
sender.go
72 lines (59 loc) · 1.39 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"bytes"
"encoding/gob"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
zmq "github.com/pebbe/zmq4"
)
var dbPath = "/home/liuruoyu/Desktop/github/sakura/sender/tables.sqlite"
type ANSWER struct {
ID uint64 `gorm:"column:id;primary_key"`
QuestionId uint64 `gorm:"column:question_id" sql:"type:integer"`
AnswerId uint64 `gorm:"column:answer_id" sql:"type:integer"`
Question string `gorm:"column:question" sql:"type:text"`
Answer string `gorm:"column:answer" sql:"type:text"`
Star uint64 `gorm:"column:star" sql:"type:integer"`
}
func (ANSWER) TableName() string {
return "answer"
}
func (a *ANSWER) AfterCreate() (err error) {
requester, err := zmq.NewSocket(zmq.REQ)
if err != nil {
return
}
defer requester.Close()
requester.Connect("tcp://localhost:5555")
var msg bytes.Buffer
encoder := gob.NewEncoder(&msg)
encoder.Encode(*a)
requester.Send(msg.String(), 0)
reply, err := requester.Recv(0)
if err != nil {
return err
}
fmt.Println(reply)
return
}
func main() {
db, err := gorm.Open("sqlite3", dbPath)
if err != nil {
panic(err)
}
defer db.Close()
tx := db.Begin()
answer := ANSWER{
QuestionId: 111111,
AnswerId: 2222222222,
Question: "长得丑怎么活?",
Answer: "长得丑还不去死!!!",
Star: 1000,
}
if err := tx.Create(&answer).Error; err != nil {
tx.Rollback()
panic(err)
}
tx.Commit()
}