Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created January 21, 2021 20:12
Show Gist options
  • Save xeoncross/5d33baed9c413ef1f2199abc39420473 to your computer and use it in GitHub Desktop.
Save xeoncross/5d33baed9c413ef1f2199abc39420473 to your computer and use it in GitHub Desktop.
VSCode Go snippets
{
// Place your snippets for go here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"if err != nil": {
"prefix": "ie",
"body": "if err != nil {\n\t${0}\n}",
"description": "Snippet for if err != nil"
},
"HTTP error response": {
"prefix": "he",
"body": "http.Error(w, http.StatusText(http.Status$1), http.Status$1)",
"description": "Snippet returning HTTP error and status"
},
"JSON Inspect": {
"prefix": "ji",
"body": "{\n\tenc := json.NewEncoder(os.Stderr)\n\tenc.SetIndent(\"\", \" \")\n\tenc.Encode($0)\n}",
"description": "Dump object as formatted JSON"
},
"http.HandlerFunc": {
"prefix": "hh",
"body": "func (w http.ResponseWriter, r *http.Request) {\n\t${0}\n}",
"description": "http.HandlerFunc body"
},
"table driven test": {
"prefix": "tdt",
"body": "func Test$1(t *testing.T) {\n\ttestCases := []struct {\n\t\tdesc\tstring\n\t\t$2\n\t}{\n\t\t{\n\t\t\tdesc: \"$3\",\n\t\t\t$4\n\t\t},\n\t}\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.desc, func(t *testing.T) {\n\t\t\t$0\n\t\t})\n\t}\n}",
"description": "Snippet for table driven test"
},
"mysql get or create": {
"prefix": "mgoc",
"body": "// GetOrCreate$1 for given domain\nfunc (q *Store) GetOrCreate$1(ctx context.Context, $2 string) (uint64, error) {\n\n\tid, err := q.Get$4(ctx, $2)\n\tif err == nil {\n\t\treturn id, nil\n\t}\n\n\t// Some actual database error?\n\tif !errors.Is(err, sql.ErrNoRows) {\n\t\treturn 0, err\n\t}\n\n\t// Try to create it\n\tid, err = q.Create$5(ctx, $2)\n\tif err == nil {\n\t\treturn id, nil\n\t}\n\n\t// Disabled because a connection error could also cause an issue and is currently\n\t// safer to just retry given the low work-load\n\t//\n\t// If we fail to insert, did another goroutine created this already?\n\t// if mysqlError, ok := err.(*mysql.MySQLError); ok {\n\t// \tif mysqlError.Number == ER_DUP_ENTRY {\n\t// \t\t// Another goroutine created this entry since we first checked\n\t// \t\tid, err = q.Get$4(ctx, $2)\n\t// \t}\n\t// }\n\n\t// Must have been created between when we first tried to\n\t// load it and when we tried to insert it - or actual DB error\n\tid, err2 := q.Get$4(ctx, $2)\n\tif err2 == nil {\n\t\treturn id, nil\n\t}\n\n\t// Most of the time this means the insert had a syntax/param error\n\tif errors.Is(err2, sql.ErrNoRows) {\n\t\treturn 0, err\n\t}\n\n\treturn 0, err\n}",
"description": "wrapper function to get or create an entity"
},
"mysql exec": {
"prefix": "me",
"body": "\n// $1 returns rows affected\nfunc (q *Store) $1(ctx context.Context, $2) (int, error) {\n\t\n\tquery := `$3`\n\n\tresult, err := q.db.ExecContext(ctx, query, $2)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"store: $1: %w\", err)\n\t}\n\n\tnum, err := result.RowsAffected()\n\tif err != nil {\n\t\treturn return 0, fmt.Errorf(\"store: $1: %w\", err)\n\t}\n\t\n\treturn num, nil\n}",
"description": "MySQL execute query"
},
"mysql select one": {
"prefix": "mso",
"body": "// $1 runs a custom query, returning $3\nfunc (q *Store) $1(ctx context.Context, $2) ($3, error) {\n\n\tquery := `SELECT`\n\n\trow := q.db.QueryRowContext(ctx, query, $4)\n\terr := row.Scan($5)\n\n\tif err != nil {\n\t\terr = fmt.Errorf(\"store: $1: %w\", err)\n\t}\n\n\treturn $6, err\n}",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment