@@ -23,18 +23,18 @@ You can use it as a MySQL slave to sync binlog from master then do something, li
23
23
24
24
``` go
25
25
import (
26
- " github.com/siddontang/go-mysql/replication"
27
- " os"
26
+ " github.com/siddontang/go-mysql/replication"
27
+ " os"
28
28
)
29
29
// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's.
30
30
// flavor is mysql or mariadb
31
31
cfg := replication.BinlogSyncerConfig {
32
- ServerID : 100 ,
33
- Flavor : " mysql" ,
34
- Host : " 127.0.0.1" ,
35
- Port : 3306 ,
36
- User : " root" ,
37
- Password : " " ,
32
+ ServerID : 100 ,
33
+ Flavor : " mysql" ,
34
+ Host : " 127.0.0.1" ,
35
+ Port : 3306 ,
36
+ User : " root" ,
37
+ Password : " " ,
38
38
}
39
39
syncer := replication.NewBinlogSyncer (cfg)
40
40
@@ -47,23 +47,23 @@ streamer, _ := syncer.StartSync(mysql.Position{binlogFile, binlogPos})
47
47
// the mariadb GTID set likes this "0-1-100"
48
48
49
49
for {
50
- ev , _ := streamer.GetEvent (context.Background ())
51
- // Dump event
52
- ev.Dump (os.Stdout )
50
+ ev , _ := streamer.GetEvent (context.Background ())
51
+ // Dump event
52
+ ev.Dump (os.Stdout )
53
53
}
54
54
55
55
// or we can use a timeout context
56
56
for {
57
- ctx , cancel := context.WithTimeout (context.Background (), 2 *time.Second )
58
- ev , err := s.GetEvent (ctx)
59
- cancel ()
57
+ ctx , cancel := context.WithTimeout (context.Background (), 2 *time.Second )
58
+ ev , err := s.GetEvent (ctx)
59
+ cancel ()
60
60
61
- if err == context.DeadlineExceeded {
62
- // meet timeout
63
- continue
64
- }
61
+ if err == context.DeadlineExceeded {
62
+ // meet timeout
63
+ continue
64
+ }
65
65
66
- ev.Dump (os.Stdout )
66
+ ev.Dump (os.Stdout )
67
67
}
68
68
```
69
69
@@ -117,16 +117,16 @@ cfg.Dump.Tables = []string{"canal_test"}
117
117
c , err := NewCanal (cfg)
118
118
119
119
type MyEventHandler struct {
120
- DummyEventHandler
120
+ DummyEventHandler
121
121
}
122
122
123
123
func (h *MyEventHandler ) OnRow (e *RowsEvent ) error {
124
- log.Infof (" %s %v \n " , e.Action , e.Rows )
125
- return nil
124
+ log.Infof (" %s %v \n " , e.Action , e.Rows )
125
+ return nil
126
126
}
127
127
128
128
func (h *MyEventHandler ) String () string {
129
- return " MyEventHandler"
129
+ return " MyEventHandler"
130
130
}
131
131
132
132
// Register a handler to handle RowsEvent
@@ -146,7 +146,7 @@ Client package supports a simple MySQL connection driver which you can use it to
146
146
147
147
``` go
148
148
import (
149
- " github.com/siddontang/go-mysql/client"
149
+ " github.com/siddontang/go-mysql/client"
150
150
)
151
151
152
152
// Connect MySQL at 127.0.0.1:3306, with user root, an empty password and database test
@@ -155,7 +155,7 @@ conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test")
155
155
// Or to use SSL/TLS connection if MySQL server supports TLS
156
156
// conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.UseSSL(true)})
157
157
158
- // or to set your own client-side certificates for identity verification for security
158
+ // Or to set your own client-side certificates for identity verification for security
159
159
// tlsConfig := NewClientTLSConfig(caPem, certPem, keyPem, false, "your-server-name")
160
160
// conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.SetTLSConfig(tlsConfig)})
161
161
@@ -181,13 +181,13 @@ v, _ = r.GetIntByName(0, "id")
181
181
182
182
// Direct access to fields
183
183
for _ , row := range r.Values {
184
- for _ , val := range row {
185
- _ = val.Value () // interface{}
186
- // or
187
- if val.Type == mysql.FieldValueTypeFloat {
188
- _ = val.AsFloat64 () // float64
189
- }
190
- }
184
+ for _ , val := range row {
185
+ _ = val.Value () // interface{}
186
+ // or
187
+ if val.Type == mysql.FieldValueTypeFloat {
188
+ _ = val.AsFloat64 () // float64
189
+ }
190
+ }
191
191
}
192
192
```
193
193
@@ -207,8 +207,8 @@ so that most MySQL clients should be able to connect to the Server without modif
207
207
208
208
``` go
209
209
import (
210
- " github.com/siddontang/go-mysql/server"
211
- " net"
210
+ " github.com/siddontang/go-mysql/server"
211
+ " net"
212
212
)
213
213
214
214
l , _ := net.Listen (" tcp" , " 127.0.0.1:4000" )
@@ -220,7 +220,7 @@ c, _ := l.Accept()
220
220
conn , _ := server.NewConn (c, " root" , " " , server.EmptyHandler {})
221
221
222
222
for {
223
- conn.HandleCommand ()
223
+ conn.HandleCommand ()
224
224
}
225
225
```
226
226
@@ -259,16 +259,16 @@ Driver is the package that you can use go-mysql with go database/sql like other
259
259
package main
260
260
261
261
import (
262
- " database/sql"
262
+ " database/sql"
263
263
264
- _ " github.com/siddontang/go-mysql/driver"
264
+ _ " github.com/siddontang/go-mysql/driver"
265
265
)
266
266
267
267
func main () {
268
- // dsn format: "user:password@addr?dbname"
269
- dsn := " [email protected] :3306?test"
270
- db , _ := sql.Open (dsn)
271
- db.Close ()
268
+ // dsn format: "user:password@addr?dbname"
269
+ dsn := " [email protected] :3306?test"
270
+ db , _ := sql.Open (dsn)
271
+ db.Close ()
272
272
}
273
273
```
274
274
0 commit comments