type Person struct { Age int `form:"age" binding:"required,gt=10"` Name string `form:"name" binding:"required"` Address string `form:"address" binding:"required"` }
type Person struct { Age int `form:"Age" binding:"required,gt=10"` Name string `form:"Name" binding:"required"` Address string `form:"Address" binding:"required"` }
func main() { r := gin.Default() r.GET("/test", testHandler) r.Run() }
func testHandler(c *gin.Context) { var person Person //根据请求content-type来作不同的binding操作 if err := c.ShouldBind(&person); err != nil { c.String(500, "%v", err) c.Abort() return } c.String(200, "%v", person)
}
1 2 3 4 5 6
➜ ~ curl -X GET "http://127.0.0.1:8080/test?age=19&name=samtake&address=gd" Key: 'Person.Age' Error:Field validation for'Age' failed on the 'required' tag Key: 'Person.Name' Error:Field validation for'Name' failed on the 'required' tag Key: 'Person.Address' Error:Field validation for'Address' failed on the 'required' tag% ➜ ~ curl -X GET "http://127.0.0.1:8080/test?Age=19&Name=samtake&Address=gd" {19 samtake gd}%
func customFunc(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { if date, ok := field.Interface().(time.Time); ok { today := time.Now() if date.Unix() > today.Unix() { return true } } return false }
func main() { r := gin.Default() if v, ok := binding.Validator.Engine().(*validator.Validate); ok { v.RegisterValidation("bookabledate", customFunc) } r.GET("/bookable", testHandler) r.Run() }
func testHandler(c *gin.Context) { var b Booking if err := c.ShouldBind(&b); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"message": "ok!", "booking": b}) }
这里有个报错,我自己也还没弄明白,然而我看了下源码,上面的使用方法是正确的:
1 2 3 4
# command-line-arguments validCustom/validCustom.go:30:24: cannot use customFunc (type func(*validator.Validate, reflect.Value, reflect.Value, reflect.Value, reflect.Typ e, reflect.Kind, string) bool) as type validator.Func in argument to v.RegisterValidation `
1 2 3 4 5 6 7 8
// RegisterValidation adds a validation with the given tag // // NOTES: // - if the key already exists, the previous validation function will be replaced. // - this method is not thread-safe it is intended that these all be registered prior to any validation func (v *Validate) RegisterValidation(tag string, fn Func) error { return v.RegisterValidationCtx(tag, wrapFunc(fn)) }
The path /www/rabbitmq is not shared from OS X and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> File Sharing. See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.
1 2
➜ ~ docker ps | grep rabbit 75d0ec6821fc rabbitmq:management "docker-entrypoint.s…" About a minute ago Up About a minute 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbit-node1
go func() { // 2.循环读取channel的数据 for d := range msgs { //3.调用callback方法来处理新的消息 processErr := callback(d.Body) if processErr { // TODO: 将任务写入另一个队列,用于异常情况的重试 } } }()
➜ Desktop docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d1f67d1e5fda redis "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp redis-test 0eee2c1708be mysql:5.7 "docker-entrypoint.s…" 8 hours ago Up 8 hours 0.0.0.0:3306->3306/tcp, 33060/tcp happy_chandrasekhar
127.0.0.1:6379> set name longshan OK 127.0.0.1:6379> get name "longshan" 127.0.0.1:6379> del name (integer) 1 127.0.0.1:6379> get name (nil) 127.0.0.1:6379>
LIST
命令
行为
rpush
获取存储在给定键中的值
lrange
设置存储在给定键中的值
lindex
删除存储在给定键中的值(这个命令可以用于所有类型)
lpop
删除存储在给定键中的值(这个命令可以用于所有类型)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
127.0.0.1:6379> lrange list 0-1 (error) ERR wrong number of arguments for'lrange'command 127.0.0.1:6379> lrange list 0 -1 1) "item1" 2) "item2" 3) "item3" 127.0.0.1:6379> lrange list 1 (error) ERR wrong number of arguments for'lrange'command 127.0.0.1:6379> lindex list 1 "item2" 127.0.0.1:6379> lpop list "item1" 127.0.0.1:6379> lrange list 0 -1 1) "item2" 2) "item3" 127.0.0.1:6379>
➜ blogs docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0eee2c1708be mysql:5.7 "docker-entrypoint.s…" 41 minutes ago Up 41 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp happy_chandrasekhar ➜ blogs docker exec -it 0eee2c1708be bash root@0eee2c1708be:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 765627 Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
mysql> status; -------------- mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
Connection id: 765627 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.29 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 1 hour 2 min 0 sec
Threads: 2 Questions: 6 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.001 -------------- mysql>
# 192.168.1.xx 是你本机的内网ip mysql -u root -h 192.168.1.xx -P13306 -p123456
在mysql client中执行 (创建用于访问主节点来同步数据的帐号)
1 2 3 4
mysql> create user slave identified by 'slave'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave'; mysql> flush privileges; mysql> create database fileserver default character set utf8mb4;