Last active
April 15, 2020 02:45
-
-
Save jony4/de09cf52904ff513edd734a6f608845d to your computer and use it in GitHub Desktop.
vscode go 语言 快捷键;打开路径 "Code" -> "Preferences" -> "User Snippets"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"fmt.Sprintf": { | |
"prefix": "fsf", | |
"body": [ | |
"fmt.Sprintf(${1:\"%v\"}, ${2:var})", | |
], | |
"description": "fmt.Sprintf" | |
}, | |
"iflen": { | |
"prefix": "ifl", | |
"body": [ | |
"if len(${1:var}) == 0 {", | |
"", | |
"}" | |
], | |
"description": "if len" | |
}, | |
"ifok": { | |
"prefix": "ifok", | |
"body": [ | |
"if _, ok := var[i]; !ok {", | |
"", | |
"}" | |
] | |
}, | |
"forr": { | |
"prefix": "forr", | |
"description": "for range by myself", | |
"body": [ | |
"for _, v := range vars {", | |
"", | |
"}" | |
] | |
}, | |
"mysqladd": { | |
"prefix": "madd", | |
"description": "mysql add func", | |
"body": [ | |
"// AddFoo ..", | |
"func (dao *Dao) AddFoo(ctx context.Context, req *model.Foo) (*model.Foo, error) {", | |
"\tres, err := dao.MySQL.ExecContext(ctx,", | |
"\t\t`sql here`,", | |
"\t\treq.Example,", | |
"\t)", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\tlastid, err := res.LastInsertId()", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\treq.ID = lastid", | |
"\treturn req, nil", | |
"}" | |
] | |
}, | |
"mysqlupdate": { | |
"prefix": "mupdate", | |
"description": "mysql update func", | |
"body": [ | |
"// UpdateFoo ..", | |
"func (dao *Dao) UpdateFoo(ctx context.Context, req *model.Foo) (*model.Foo, error) {", | |
"\tnow := time.Now().UnixNano() / 1000 / 1000", | |
"\tres, err := dao.MySQL.ExecContext(ctx, ", | |
"\t\t`sql here`,", | |
"\t\treq.Example,", | |
"\t\tnow,", | |
"\t)", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\trfid, err := res.RowsAffected()", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\tif rfid == 0 {", | |
"\t\treturn nil, errors.New(\"update rfid == 0\")", | |
"\t}", | |
"\tnewFoo, err := dao.ViewFoo(ctx, req)", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\treturn newFoo, nil", | |
"}" | |
] | |
}, | |
"mysqldel": { | |
"prefix": "mdel", | |
"description": "mysql delete func", | |
"body": [ | |
"// DeleteFoo ..", | |
"func (dao *Dao) DeleteFoo(ctx context.Context, req *model.Foo) (int64, error) {", | |
"\tret, err := dao.MySQL.ExecContext(ctx,", | |
"\t\t`sql here`,", | |
"\t\treq.ID,", | |
"\t)", | |
"\tif err != nil {", | |
"\t\treturn 0, err", | |
"\t}", | |
"\treturn ret.RowsAffected()", | |
"}" | |
] | |
}, | |
"mysqlview": { | |
"prefix": "mview", | |
"description": "mysql view func", | |
"body": [ | |
"// ViewFoo ..", | |
"func (dao *Dao) ViewFoo(ctx context.Context, req *model.Foo) (*model.Foo, error) {", | |
"\tif err := dao.MySQL.QueryRowContext(ctx,", | |
"\t\t`sql here`,", | |
"\t\treq.Userid, req.Id,", | |
"\t).Scan(", | |
"\t\t&req.ID,", | |
"\t); err != nil {", | |
"\t\tif err == sql.ErrNoRows {", | |
"\t\t\treturn req, nil", | |
"\t\t}", | |
"\t\treturn nil, err", | |
"\t}", | |
"\treturn req, nil", | |
"}" | |
] | |
}, | |
"mysqllist": { | |
"prefix": "mlist", | |
"description": "mysql list func", | |
"body": [ | |
"// ListFoo ..", | |
"func (dao *Dao) ListFoo(ctx context.Context, req *model.Foo) ([]*model.Foo, error) {", | |
"\tfoos := []*model.Foo{}", | |
"\trows, err := dao.MySQL.QueryContext(ctx,", | |
"\t\t`sql here`,", | |
"\t\treq.ID)", | |
"\tif err != nil {", | |
"\t\treturn nil, err", | |
"\t}", | |
"\tdefer rows.Close()", | |
"\tfor rows.Next() {", | |
"\t\tfoo := &model.Foo{}", | |
"\t\tif err := rows.Scan(", | |
"\t\t\t&foo.ID,", | |
"\t\t); err != nil {", | |
"\t\t\treturn nil, err", | |
"\t\t}", | |
"\t\tfoos = append(foos, foo)", | |
"\t}", | |
"\treturn foos, nil", | |
"}" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment