repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
list | docstring
stringlengths 6
2.61k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 85
252
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
sajari/docconv
|
xml.go
|
ConvertXML
|
func ConvertXML(r io.Reader) (string, map[string]string, error) {
meta := make(map[string]string)
cleanXML, err := Tidy(r, true)
if err != nil {
return "", nil, fmt.Errorf("tidy error: %v", err)
}
result, err := XMLToText(bytes.NewReader(cleanXML), []string{}, []string{}, true)
if err != nil {
return "", nil, fmt.Errorf("error from XMLToText: %v", err)
}
return result, meta, nil
}
|
go
|
func ConvertXML(r io.Reader) (string, map[string]string, error) {
meta := make(map[string]string)
cleanXML, err := Tidy(r, true)
if err != nil {
return "", nil, fmt.Errorf("tidy error: %v", err)
}
result, err := XMLToText(bytes.NewReader(cleanXML), []string{}, []string{}, true)
if err != nil {
return "", nil, fmt.Errorf("error from XMLToText: %v", err)
}
return result, meta, nil
}
|
[
"func",
"ConvertXML",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"string",
",",
"map",
"[",
"string",
"]",
"string",
",",
"error",
")",
"{",
"meta",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"cleanXML",
",",
"err",
":=",
"Tidy",
"(",
"r",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"result",
",",
"err",
":=",
"XMLToText",
"(",
"bytes",
".",
"NewReader",
"(",
"cleanXML",
")",
",",
"[",
"]",
"string",
"{",
"}",
",",
"[",
"]",
"string",
"{",
"}",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"return",
"result",
",",
"meta",
",",
"nil",
"\n",
"}"
] |
// ConvertXML converts an XML file to text.
|
[
"ConvertXML",
"converts",
"an",
"XML",
"file",
"to",
"text",
"."
] |
eedabc494f8e97c31f94c70453273f16dbecbc6f
|
https://github.com/sajari/docconv/blob/eedabc494f8e97c31f94c70453273f16dbecbc6f/xml.go#L11-L22
|
train
|
sajari/docconv
|
xml.go
|
XMLToText
|
func XMLToText(r io.Reader, breaks []string, skip []string, strict bool) (string, error) {
var result string
dec := xml.NewDecoder(r)
dec.Strict = strict
for {
t, err := dec.Token()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
switch v := t.(type) {
case xml.CharData:
result += string(v)
case xml.StartElement:
for _, breakElement := range breaks {
if v.Name.Local == breakElement {
result += "\n"
}
}
for _, skipElement := range skip {
if v.Name.Local == skipElement {
depth := 1
for {
t, err := dec.Token()
if err != nil {
// An io.EOF here is actually an error.
return "", err
}
switch t.(type) {
case xml.StartElement:
depth++
case xml.EndElement:
depth--
}
if depth == 0 {
break
}
}
}
}
}
}
return result, nil
}
|
go
|
func XMLToText(r io.Reader, breaks []string, skip []string, strict bool) (string, error) {
var result string
dec := xml.NewDecoder(r)
dec.Strict = strict
for {
t, err := dec.Token()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
switch v := t.(type) {
case xml.CharData:
result += string(v)
case xml.StartElement:
for _, breakElement := range breaks {
if v.Name.Local == breakElement {
result += "\n"
}
}
for _, skipElement := range skip {
if v.Name.Local == skipElement {
depth := 1
for {
t, err := dec.Token()
if err != nil {
// An io.EOF here is actually an error.
return "", err
}
switch t.(type) {
case xml.StartElement:
depth++
case xml.EndElement:
depth--
}
if depth == 0 {
break
}
}
}
}
}
}
return result, nil
}
|
[
"func",
"XMLToText",
"(",
"r",
"io",
".",
"Reader",
",",
"breaks",
"[",
"]",
"string",
",",
"skip",
"[",
"]",
"string",
",",
"strict",
"bool",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"result",
"string",
"\n\n",
"dec",
":=",
"xml",
".",
"NewDecoder",
"(",
"r",
")",
"\n",
"dec",
".",
"Strict",
"=",
"strict",
"\n",
"for",
"{",
"t",
",",
"err",
":=",
"dec",
".",
"Token",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"break",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"switch",
"v",
":=",
"t",
".",
"(",
"type",
")",
"{",
"case",
"xml",
".",
"CharData",
":",
"result",
"+=",
"string",
"(",
"v",
")",
"\n",
"case",
"xml",
".",
"StartElement",
":",
"for",
"_",
",",
"breakElement",
":=",
"range",
"breaks",
"{",
"if",
"v",
".",
"Name",
".",
"Local",
"==",
"breakElement",
"{",
"result",
"+=",
"\"",
"\\n",
"\"",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"_",
",",
"skipElement",
":=",
"range",
"skip",
"{",
"if",
"v",
".",
"Name",
".",
"Local",
"==",
"skipElement",
"{",
"depth",
":=",
"1",
"\n",
"for",
"{",
"t",
",",
"err",
":=",
"dec",
".",
"Token",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"// An io.EOF here is actually an error.",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"switch",
"t",
".",
"(",
"type",
")",
"{",
"case",
"xml",
".",
"StartElement",
":",
"depth",
"++",
"\n",
"case",
"xml",
".",
"EndElement",
":",
"depth",
"--",
"\n",
"}",
"\n\n",
"if",
"depth",
"==",
"0",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"result",
",",
"nil",
"\n",
"}"
] |
// XMLToText converts XML to plain text given how to treat elements.
|
[
"XMLToText",
"converts",
"XML",
"to",
"plain",
"text",
"given",
"how",
"to",
"treat",
"elements",
"."
] |
eedabc494f8e97c31f94c70453273f16dbecbc6f
|
https://github.com/sajari/docconv/blob/eedabc494f8e97c31f94c70453273f16dbecbc6f/xml.go#L25-L74
|
train
|
sajari/docconv
|
xml.go
|
XMLToMap
|
func XMLToMap(r io.Reader) (map[string]string, error) {
m := make(map[string]string)
dec := xml.NewDecoder(r)
var tagName string
for {
t, err := dec.Token()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
switch v := t.(type) {
case xml.StartElement:
tagName = string(v.Name.Local)
case xml.CharData:
m[tagName] = string(v)
}
}
return m, nil
}
|
go
|
func XMLToMap(r io.Reader) (map[string]string, error) {
m := make(map[string]string)
dec := xml.NewDecoder(r)
var tagName string
for {
t, err := dec.Token()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
switch v := t.(type) {
case xml.StartElement:
tagName = string(v.Name.Local)
case xml.CharData:
m[tagName] = string(v)
}
}
return m, nil
}
|
[
"func",
"XMLToMap",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"map",
"[",
"string",
"]",
"string",
",",
"error",
")",
"{",
"m",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"dec",
":=",
"xml",
".",
"NewDecoder",
"(",
"r",
")",
"\n",
"var",
"tagName",
"string",
"\n",
"for",
"{",
"t",
",",
"err",
":=",
"dec",
".",
"Token",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"break",
"\n",
"}",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"switch",
"v",
":=",
"t",
".",
"(",
"type",
")",
"{",
"case",
"xml",
".",
"StartElement",
":",
"tagName",
"=",
"string",
"(",
"v",
".",
"Name",
".",
"Local",
")",
"\n",
"case",
"xml",
".",
"CharData",
":",
"m",
"[",
"tagName",
"]",
"=",
"string",
"(",
"v",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"m",
",",
"nil",
"\n",
"}"
] |
// XMLToMap converts XML to a nested string map.
|
[
"XMLToMap",
"converts",
"XML",
"to",
"a",
"nested",
"string",
"map",
"."
] |
eedabc494f8e97c31f94c70453273f16dbecbc6f
|
https://github.com/sajari/docconv/blob/eedabc494f8e97c31f94c70453273f16dbecbc6f/xml.go#L77-L98
|
train
|
go-pg/pg
|
orm/delete.go
|
ForceDelete
|
func ForceDelete(db DB, model interface{}) error {
res, err := NewQuery(db, model).WherePK().ForceDelete()
if err != nil {
return err
}
return internal.AssertOneRow(res.RowsAffected())
}
|
go
|
func ForceDelete(db DB, model interface{}) error {
res, err := NewQuery(db, model).WherePK().ForceDelete()
if err != nil {
return err
}
return internal.AssertOneRow(res.RowsAffected())
}
|
[
"func",
"ForceDelete",
"(",
"db",
"DB",
",",
"model",
"interface",
"{",
"}",
")",
"error",
"{",
"res",
",",
"err",
":=",
"NewQuery",
"(",
"db",
",",
"model",
")",
".",
"WherePK",
"(",
")",
".",
"ForceDelete",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"internal",
".",
"AssertOneRow",
"(",
"res",
".",
"RowsAffected",
"(",
")",
")",
"\n",
"}"
] |
// ForceDelete force deletes a given model from the db
|
[
"ForceDelete",
"force",
"deletes",
"a",
"given",
"model",
"from",
"the",
"db"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/delete.go#L17-L23
|
train
|
go-pg/pg
|
urlvalues/decode.go
|
Decode
|
func Decode(strct interface{}, values Values) error {
v := reflect.Indirect(reflect.ValueOf(strct))
meta := structfilter.GetStruct(v.Type())
for name, values := range values {
name = strings.TrimPrefix(name, ":")
name = strings.TrimSuffix(name, "[]")
field := meta.Field(name)
if field != nil && !field.NoDecode() {
err := field.Scan(field.Value(v), values)
if err != nil {
return err
}
}
}
return nil
}
|
go
|
func Decode(strct interface{}, values Values) error {
v := reflect.Indirect(reflect.ValueOf(strct))
meta := structfilter.GetStruct(v.Type())
for name, values := range values {
name = strings.TrimPrefix(name, ":")
name = strings.TrimSuffix(name, "[]")
field := meta.Field(name)
if field != nil && !field.NoDecode() {
err := field.Scan(field.Value(v), values)
if err != nil {
return err
}
}
}
return nil
}
|
[
"func",
"Decode",
"(",
"strct",
"interface",
"{",
"}",
",",
"values",
"Values",
")",
"error",
"{",
"v",
":=",
"reflect",
".",
"Indirect",
"(",
"reflect",
".",
"ValueOf",
"(",
"strct",
")",
")",
"\n",
"meta",
":=",
"structfilter",
".",
"GetStruct",
"(",
"v",
".",
"Type",
"(",
")",
")",
"\n\n",
"for",
"name",
",",
"values",
":=",
"range",
"values",
"{",
"name",
"=",
"strings",
".",
"TrimPrefix",
"(",
"name",
",",
"\"",
"\"",
")",
"\n",
"name",
"=",
"strings",
".",
"TrimSuffix",
"(",
"name",
",",
"\"",
"\"",
")",
"\n\n",
"field",
":=",
"meta",
".",
"Field",
"(",
"name",
")",
"\n",
"if",
"field",
"!=",
"nil",
"&&",
"!",
"field",
".",
"NoDecode",
"(",
")",
"{",
"err",
":=",
"field",
".",
"Scan",
"(",
"field",
".",
"Value",
"(",
"v",
")",
",",
"values",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Decode decodes url values into the struct.
|
[
"Decode",
"decodes",
"url",
"values",
"into",
"the",
"struct",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/urlvalues/decode.go#L11-L29
|
train
|
go-pg/pg
|
options.go
|
ParseURL
|
func ParseURL(sURL string) (*Options, error) {
parsedURL, err := url.Parse(sURL)
if err != nil {
return nil, err
}
// scheme
if parsedURL.Scheme != "postgres" && parsedURL.Scheme != "postgresql" {
return nil, errors.New("pg: invalid scheme: " + parsedURL.Scheme)
}
// host and port
options := &Options{
Addr: parsedURL.Host,
}
if !strings.Contains(options.Addr, ":") {
options.Addr += ":5432"
}
// username and password
if parsedURL.User != nil {
options.User = parsedURL.User.Username()
if password, ok := parsedURL.User.Password(); ok {
options.Password = password
}
}
if options.User == "" {
options.User = "postgres"
}
// database
if len(strings.Trim(parsedURL.Path, "/")) > 0 {
options.Database = parsedURL.Path[1:]
} else {
return nil, errors.New("pg: database name not provided")
}
// ssl mode
query, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return nil, err
}
if sslMode, ok := query["sslmode"]; ok && len(sslMode) > 0 {
switch sslMode[0] {
case "allow", "prefer", "require":
options.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint
case "disable":
options.TLSConfig = nil
default:
return nil, fmt.Errorf("pg: sslmode '%v' is not supported", sslMode[0])
}
} else {
options.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint
}
delete(query, "sslmode")
if appName, ok := query["application_name"]; ok && len(appName) > 0 {
options.ApplicationName = appName[0]
}
delete(query, "application_name")
if len(query) > 0 {
return nil, errors.New("pg: options other than 'sslmode' and 'application_name' are not supported")
}
return options, nil
}
|
go
|
func ParseURL(sURL string) (*Options, error) {
parsedURL, err := url.Parse(sURL)
if err != nil {
return nil, err
}
// scheme
if parsedURL.Scheme != "postgres" && parsedURL.Scheme != "postgresql" {
return nil, errors.New("pg: invalid scheme: " + parsedURL.Scheme)
}
// host and port
options := &Options{
Addr: parsedURL.Host,
}
if !strings.Contains(options.Addr, ":") {
options.Addr += ":5432"
}
// username and password
if parsedURL.User != nil {
options.User = parsedURL.User.Username()
if password, ok := parsedURL.User.Password(); ok {
options.Password = password
}
}
if options.User == "" {
options.User = "postgres"
}
// database
if len(strings.Trim(parsedURL.Path, "/")) > 0 {
options.Database = parsedURL.Path[1:]
} else {
return nil, errors.New("pg: database name not provided")
}
// ssl mode
query, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return nil, err
}
if sslMode, ok := query["sslmode"]; ok && len(sslMode) > 0 {
switch sslMode[0] {
case "allow", "prefer", "require":
options.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint
case "disable":
options.TLSConfig = nil
default:
return nil, fmt.Errorf("pg: sslmode '%v' is not supported", sslMode[0])
}
} else {
options.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint
}
delete(query, "sslmode")
if appName, ok := query["application_name"]; ok && len(appName) > 0 {
options.ApplicationName = appName[0]
}
delete(query, "application_name")
if len(query) > 0 {
return nil, errors.New("pg: options other than 'sslmode' and 'application_name' are not supported")
}
return options, nil
}
|
[
"func",
"ParseURL",
"(",
"sURL",
"string",
")",
"(",
"*",
"Options",
",",
"error",
")",
"{",
"parsedURL",
",",
"err",
":=",
"url",
".",
"Parse",
"(",
"sURL",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"// scheme",
"if",
"parsedURL",
".",
"Scheme",
"!=",
"\"",
"\"",
"&&",
"parsedURL",
".",
"Scheme",
"!=",
"\"",
"\"",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"parsedURL",
".",
"Scheme",
")",
"\n",
"}",
"\n\n",
"// host and port",
"options",
":=",
"&",
"Options",
"{",
"Addr",
":",
"parsedURL",
".",
"Host",
",",
"}",
"\n",
"if",
"!",
"strings",
".",
"Contains",
"(",
"options",
".",
"Addr",
",",
"\"",
"\"",
")",
"{",
"options",
".",
"Addr",
"+=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"// username and password",
"if",
"parsedURL",
".",
"User",
"!=",
"nil",
"{",
"options",
".",
"User",
"=",
"parsedURL",
".",
"User",
".",
"Username",
"(",
")",
"\n\n",
"if",
"password",
",",
"ok",
":=",
"parsedURL",
".",
"User",
".",
"Password",
"(",
")",
";",
"ok",
"{",
"options",
".",
"Password",
"=",
"password",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"options",
".",
"User",
"==",
"\"",
"\"",
"{",
"options",
".",
"User",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"// database",
"if",
"len",
"(",
"strings",
".",
"Trim",
"(",
"parsedURL",
".",
"Path",
",",
"\"",
"\"",
")",
")",
">",
"0",
"{",
"options",
".",
"Database",
"=",
"parsedURL",
".",
"Path",
"[",
"1",
":",
"]",
"\n",
"}",
"else",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// ssl mode",
"query",
",",
"err",
":=",
"url",
".",
"ParseQuery",
"(",
"parsedURL",
".",
"RawQuery",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"sslMode",
",",
"ok",
":=",
"query",
"[",
"\"",
"\"",
"]",
";",
"ok",
"&&",
"len",
"(",
"sslMode",
")",
">",
"0",
"{",
"switch",
"sslMode",
"[",
"0",
"]",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
":",
"options",
".",
"TLSConfig",
"=",
"&",
"tls",
".",
"Config",
"{",
"InsecureSkipVerify",
":",
"true",
"}",
"//nolint",
"\n",
"case",
"\"",
"\"",
":",
"options",
".",
"TLSConfig",
"=",
"nil",
"\n",
"default",
":",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"sslMode",
"[",
"0",
"]",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"options",
".",
"TLSConfig",
"=",
"&",
"tls",
".",
"Config",
"{",
"InsecureSkipVerify",
":",
"true",
"}",
"//nolint",
"\n",
"}",
"\n\n",
"delete",
"(",
"query",
",",
"\"",
"\"",
")",
"\n\n",
"if",
"appName",
",",
"ok",
":=",
"query",
"[",
"\"",
"\"",
"]",
";",
"ok",
"&&",
"len",
"(",
"appName",
")",
">",
"0",
"{",
"options",
".",
"ApplicationName",
"=",
"appName",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"delete",
"(",
"query",
",",
"\"",
"\"",
")",
"\n\n",
"if",
"len",
"(",
"query",
")",
">",
"0",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"return",
"options",
",",
"nil",
"\n",
"}"
] |
// ParseURL parses an URL into options that can be used to connect to PostgreSQL.
|
[
"ParseURL",
"parses",
"an",
"URL",
"into",
"options",
"that",
"can",
"be",
"used",
"to",
"connect",
"to",
"PostgreSQL",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/options.go#L143-L214
|
train
|
go-pg/pg
|
internal/underscore.go
|
Underscore
|
func Underscore(s string) string {
r := make([]byte, 0, len(s)+5)
for i := 0; i < len(s); i++ {
c := s[i]
if IsUpper(c) {
if i > 0 && i+1 < len(s) && (IsLower(s[i-1]) || IsLower(s[i+1])) {
r = append(r, '_', ToLower(c))
} else {
r = append(r, ToLower(c))
}
} else {
r = append(r, c)
}
}
return string(r)
}
|
go
|
func Underscore(s string) string {
r := make([]byte, 0, len(s)+5)
for i := 0; i < len(s); i++ {
c := s[i]
if IsUpper(c) {
if i > 0 && i+1 < len(s) && (IsLower(s[i-1]) || IsLower(s[i+1])) {
r = append(r, '_', ToLower(c))
} else {
r = append(r, ToLower(c))
}
} else {
r = append(r, c)
}
}
return string(r)
}
|
[
"func",
"Underscore",
"(",
"s",
"string",
")",
"string",
"{",
"r",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"0",
",",
"len",
"(",
"s",
")",
"+",
"5",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"s",
")",
";",
"i",
"++",
"{",
"c",
":=",
"s",
"[",
"i",
"]",
"\n",
"if",
"IsUpper",
"(",
"c",
")",
"{",
"if",
"i",
">",
"0",
"&&",
"i",
"+",
"1",
"<",
"len",
"(",
"s",
")",
"&&",
"(",
"IsLower",
"(",
"s",
"[",
"i",
"-",
"1",
"]",
")",
"||",
"IsLower",
"(",
"s",
"[",
"i",
"+",
"1",
"]",
")",
")",
"{",
"r",
"=",
"append",
"(",
"r",
",",
"'_'",
",",
"ToLower",
"(",
"c",
")",
")",
"\n",
"}",
"else",
"{",
"r",
"=",
"append",
"(",
"r",
",",
"ToLower",
"(",
"c",
")",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"r",
"=",
"append",
"(",
"r",
",",
"c",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"string",
"(",
"r",
")",
"\n",
"}"
] |
// Underscore converts "CamelCasedString" to "camel_cased_string".
|
[
"Underscore",
"converts",
"CamelCasedString",
"to",
"camel_cased_string",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/internal/underscore.go#L20-L35
|
train
|
go-pg/pg
|
tx.go
|
Begin
|
func (db *baseDB) Begin() (*Tx, error) {
tx := &Tx{
db: db.withPool(pool.NewSingleConnPool(db.pool)),
ctx: db.db.Context(),
}
err := tx.begin()
if err != nil {
tx.close()
return nil, err
}
return tx, nil
}
|
go
|
func (db *baseDB) Begin() (*Tx, error) {
tx := &Tx{
db: db.withPool(pool.NewSingleConnPool(db.pool)),
ctx: db.db.Context(),
}
err := tx.begin()
if err != nil {
tx.close()
return nil, err
}
return tx, nil
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Begin",
"(",
")",
"(",
"*",
"Tx",
",",
"error",
")",
"{",
"tx",
":=",
"&",
"Tx",
"{",
"db",
":",
"db",
".",
"withPool",
"(",
"pool",
".",
"NewSingleConnPool",
"(",
"db",
".",
"pool",
")",
")",
",",
"ctx",
":",
"db",
".",
"db",
".",
"Context",
"(",
")",
",",
"}",
"\n\n",
"err",
":=",
"tx",
".",
"begin",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"tx",
".",
"close",
"(",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"tx",
",",
"nil",
"\n",
"}"
] |
// Begin starts a transaction. Most callers should use RunInTransaction instead.
|
[
"Begin",
"starts",
"a",
"transaction",
".",
"Most",
"callers",
"should",
"use",
"RunInTransaction",
"instead",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L38-L51
|
train
|
go-pg/pg
|
tx.go
|
RunInTransaction
|
func (db *baseDB) RunInTransaction(fn func(*Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
return tx.RunInTransaction(fn)
}
|
go
|
func (db *baseDB) RunInTransaction(fn func(*Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
return tx.RunInTransaction(fn)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"RunInTransaction",
"(",
"fn",
"func",
"(",
"*",
"Tx",
")",
"error",
")",
"error",
"{",
"tx",
",",
"err",
":=",
"db",
".",
"Begin",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"tx",
".",
"RunInTransaction",
"(",
"fn",
")",
"\n",
"}"
] |
// RunInTransaction runs a function in a transaction. If function
// returns an error transaction is rollbacked, otherwise transaction
// is committed.
|
[
"RunInTransaction",
"runs",
"a",
"function",
"in",
"a",
"transaction",
".",
"If",
"function",
"returns",
"an",
"error",
"transaction",
"is",
"rollbacked",
"otherwise",
"transaction",
"is",
"committed",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L56-L62
|
train
|
go-pg/pg
|
tx.go
|
RunInTransaction
|
func (tx *Tx) RunInTransaction(fn func(*Tx) error) error {
defer func() {
if err := recover(); err != nil {
_ = tx.Rollback()
panic(err)
}
}()
if err := fn(tx); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
|
go
|
func (tx *Tx) RunInTransaction(fn func(*Tx) error) error {
defer func() {
if err := recover(); err != nil {
_ = tx.Rollback()
panic(err)
}
}()
if err := fn(tx); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"RunInTransaction",
"(",
"fn",
"func",
"(",
"*",
"Tx",
")",
"error",
")",
"error",
"{",
"defer",
"func",
"(",
")",
"{",
"if",
"err",
":=",
"recover",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"_",
"=",
"tx",
".",
"Rollback",
"(",
")",
"\n",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"if",
"err",
":=",
"fn",
"(",
"tx",
")",
";",
"err",
"!=",
"nil",
"{",
"_",
"=",
"tx",
".",
"Rollback",
"(",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"return",
"tx",
".",
"Commit",
"(",
")",
"\n",
"}"
] |
// RunInTransaction runs a function in the transaction. If function
// returns an error transaction is rollbacked, otherwise transaction
// is committed.
|
[
"RunInTransaction",
"runs",
"a",
"function",
"in",
"the",
"transaction",
".",
"If",
"function",
"returns",
"an",
"error",
"transaction",
"is",
"rollbacked",
"otherwise",
"transaction",
"is",
"committed",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L72-L84
|
train
|
go-pg/pg
|
tx.go
|
Stmt
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
stmt, err := tx.Prepare(stmt.q)
if err != nil {
return &Stmt{stickyErr: err}
}
return stmt
}
|
go
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
stmt, err := tx.Prepare(stmt.q)
if err != nil {
return &Stmt{stickyErr: err}
}
return stmt
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"Stmt",
"(",
"stmt",
"*",
"Stmt",
")",
"*",
"Stmt",
"{",
"stmt",
",",
"err",
":=",
"tx",
".",
"Prepare",
"(",
"stmt",
".",
"q",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"&",
"Stmt",
"{",
"stickyErr",
":",
"err",
"}",
"\n",
"}",
"\n",
"return",
"stmt",
"\n",
"}"
] |
// Stmt returns a transaction-specific prepared statement
// from an existing statement.
|
[
"Stmt",
"returns",
"a",
"transaction",
"-",
"specific",
"prepared",
"statement",
"from",
"an",
"existing",
"statement",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L96-L102
|
train
|
go-pg/pg
|
tx.go
|
ExecContext
|
func (tx *Tx) ExecContext(c context.Context, query interface{}, params ...interface{}) (Result, error) {
return tx.exec(c, query, params...)
}
|
go
|
func (tx *Tx) ExecContext(c context.Context, query interface{}, params ...interface{}) (Result, error) {
return tx.exec(c, query, params...)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"ExecContext",
"(",
"c",
"context",
".",
"Context",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"tx",
".",
"exec",
"(",
"c",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// ExecContext acts like Exec but additionally receives a context
|
[
"ExecContext",
"acts",
"like",
"Exec",
"but",
"additionally",
"receives",
"a",
"context"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L129-L131
|
train
|
go-pg/pg
|
tx.go
|
Model
|
func (tx *Tx) Model(model ...interface{}) *orm.Query {
return orm.NewQuery(tx, model...)
}
|
go
|
func (tx *Tx) Model(model ...interface{}) *orm.Query {
return orm.NewQuery(tx, model...)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"Model",
"(",
"model",
"...",
"interface",
"{",
"}",
")",
"*",
"orm",
".",
"Query",
"{",
"return",
"orm",
".",
"NewQuery",
"(",
"tx",
",",
"model",
"...",
")",
"\n",
"}"
] |
// Model is an alias for DB.Model.
|
[
"Model",
"is",
"an",
"alias",
"for",
"DB",
".",
"Model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L243-L245
|
train
|
go-pg/pg
|
tx.go
|
ModelContext
|
func (tx *Tx) ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, tx, model...)
}
|
go
|
func (tx *Tx) ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, tx, model...)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"ModelContext",
"(",
"c",
"context",
".",
"Context",
",",
"model",
"...",
"interface",
"{",
"}",
")",
"*",
"orm",
".",
"Query",
"{",
"return",
"orm",
".",
"NewQueryContext",
"(",
"c",
",",
"tx",
",",
"model",
"...",
")",
"\n",
"}"
] |
// ModelContext acts like Model but additionally receives a context
|
[
"ModelContext",
"acts",
"like",
"Model",
"but",
"additionally",
"receives",
"a",
"context"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L248-L250
|
train
|
go-pg/pg
|
tx.go
|
CreateTable
|
func (tx *Tx) CreateTable(model interface{}, opt *orm.CreateTableOptions) error {
return orm.CreateTable(tx, model, opt)
}
|
go
|
func (tx *Tx) CreateTable(model interface{}, opt *orm.CreateTableOptions) error {
return orm.CreateTable(tx, model, opt)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"CreateTable",
"(",
"model",
"interface",
"{",
"}",
",",
"opt",
"*",
"orm",
".",
"CreateTableOptions",
")",
"error",
"{",
"return",
"orm",
".",
"CreateTable",
"(",
"tx",
",",
"model",
",",
"opt",
")",
"\n",
"}"
] |
// CreateTable is an alias for DB.CreateTable.
|
[
"CreateTable",
"is",
"an",
"alias",
"for",
"DB",
".",
"CreateTable",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L278-L280
|
train
|
go-pg/pg
|
tx.go
|
DropTable
|
func (tx *Tx) DropTable(model interface{}, opt *orm.DropTableOptions) error {
return orm.DropTable(tx, model, opt)
}
|
go
|
func (tx *Tx) DropTable(model interface{}, opt *orm.DropTableOptions) error {
return orm.DropTable(tx, model, opt)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"DropTable",
"(",
"model",
"interface",
"{",
"}",
",",
"opt",
"*",
"orm",
".",
"DropTableOptions",
")",
"error",
"{",
"return",
"orm",
".",
"DropTable",
"(",
"tx",
",",
"model",
",",
"opt",
")",
"\n",
"}"
] |
// DropTable is an alias for DB.DropTable.
|
[
"DropTable",
"is",
"an",
"alias",
"for",
"DB",
".",
"DropTable",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L283-L285
|
train
|
go-pg/pg
|
tx.go
|
CopyFrom
|
func (tx *Tx) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (res Result, err error) {
err = tx.withConn(context.TODO(), func(cn *pool.Conn) error {
res, err = tx.db.copyFrom(cn, r, query, params...)
return err
})
return res, err
}
|
go
|
func (tx *Tx) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (res Result, err error) {
err = tx.withConn(context.TODO(), func(cn *pool.Conn) error {
res, err = tx.db.copyFrom(cn, r, query, params...)
return err
})
return res, err
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"CopyFrom",
"(",
"r",
"io",
".",
"Reader",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"res",
"Result",
",",
"err",
"error",
")",
"{",
"err",
"=",
"tx",
".",
"withConn",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"func",
"(",
"cn",
"*",
"pool",
".",
"Conn",
")",
"error",
"{",
"res",
",",
"err",
"=",
"tx",
".",
"db",
".",
"copyFrom",
"(",
"cn",
",",
"r",
",",
"query",
",",
"params",
"...",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"res",
",",
"err",
"\n",
"}"
] |
// CopyFrom is an alias for DB.CopyFrom.
|
[
"CopyFrom",
"is",
"an",
"alias",
"for",
"DB",
".",
"CopyFrom",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L288-L294
|
train
|
go-pg/pg
|
tx.go
|
CopyTo
|
func (tx *Tx) CopyTo(w io.Writer, query interface{}, params ...interface{}) (res Result, err error) {
err = tx.withConn(context.TODO(), func(cn *pool.Conn) error {
res, err = tx.db.copyTo(cn, w, query, params...)
return err
})
return res, err
}
|
go
|
func (tx *Tx) CopyTo(w io.Writer, query interface{}, params ...interface{}) (res Result, err error) {
err = tx.withConn(context.TODO(), func(cn *pool.Conn) error {
res, err = tx.db.copyTo(cn, w, query, params...)
return err
})
return res, err
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"CopyTo",
"(",
"w",
"io",
".",
"Writer",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"res",
"Result",
",",
"err",
"error",
")",
"{",
"err",
"=",
"tx",
".",
"withConn",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"func",
"(",
"cn",
"*",
"pool",
".",
"Conn",
")",
"error",
"{",
"res",
",",
"err",
"=",
"tx",
".",
"db",
".",
"copyTo",
"(",
"cn",
",",
"w",
",",
"query",
",",
"params",
"...",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"res",
",",
"err",
"\n",
"}"
] |
// CopyTo is an alias for DB.CopyTo.
|
[
"CopyTo",
"is",
"an",
"alias",
"for",
"DB",
".",
"CopyTo",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L297-L303
|
train
|
go-pg/pg
|
tx.go
|
FormatQuery
|
func (tx *Tx) FormatQuery(dst []byte, query string, params ...interface{}) []byte {
return tx.db.FormatQuery(dst, query, params...)
}
|
go
|
func (tx *Tx) FormatQuery(dst []byte, query string, params ...interface{}) []byte {
return tx.db.FormatQuery(dst, query, params...)
}
|
[
"func",
"(",
"tx",
"*",
"Tx",
")",
"FormatQuery",
"(",
"dst",
"[",
"]",
"byte",
",",
"query",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"[",
"]",
"byte",
"{",
"return",
"tx",
".",
"db",
".",
"FormatQuery",
"(",
"dst",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// FormatQuery is an alias for DB.FormatQuery
|
[
"FormatQuery",
"is",
"an",
"alias",
"for",
"DB",
".",
"FormatQuery"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/tx.go#L306-L308
|
train
|
go-pg/pg
|
hook.go
|
UnformattedQuery
|
func (ev *QueryEvent) UnformattedQuery() (string, error) {
b, err := queryString(ev.Query)
if err != nil {
return "", err
}
return string(b), nil
}
|
go
|
func (ev *QueryEvent) UnformattedQuery() (string, error) {
b, err := queryString(ev.Query)
if err != nil {
return "", err
}
return string(b), nil
}
|
[
"func",
"(",
"ev",
"*",
"QueryEvent",
")",
"UnformattedQuery",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"b",
",",
"err",
":=",
"queryString",
"(",
"ev",
".",
"Query",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"string",
"(",
"b",
")",
",",
"nil",
"\n",
"}"
] |
// UnformattedQuery returns the unformatted query of a query event
|
[
"UnformattedQuery",
"returns",
"the",
"unformatted",
"query",
"of",
"a",
"query",
"event"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/hook.go#L36-L42
|
train
|
go-pg/pg
|
hook.go
|
FormattedQuery
|
func (ev *QueryEvent) FormattedQuery() (string, error) {
b, err := appendQuery(ev.DB, nil, ev.Query, ev.Params...)
if err != nil {
return "", err
}
return string(b), nil
}
|
go
|
func (ev *QueryEvent) FormattedQuery() (string, error) {
b, err := appendQuery(ev.DB, nil, ev.Query, ev.Params...)
if err != nil {
return "", err
}
return string(b), nil
}
|
[
"func",
"(",
"ev",
"*",
"QueryEvent",
")",
"FormattedQuery",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"b",
",",
"err",
":=",
"appendQuery",
"(",
"ev",
".",
"DB",
",",
"nil",
",",
"ev",
".",
"Query",
",",
"ev",
".",
"Params",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"string",
"(",
"b",
")",
",",
"nil",
"\n",
"}"
] |
// FormattedQuery returns the formatted query of a query event
|
[
"FormattedQuery",
"returns",
"the",
"formatted",
"query",
"of",
"a",
"query",
"event"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/hook.go#L45-L51
|
train
|
go-pg/pg
|
hook.go
|
AddQueryHook
|
func (db *baseDB) AddQueryHook(hook QueryHook) {
db.queryHooks = append(db.queryHooks, hook)
}
|
go
|
func (db *baseDB) AddQueryHook(hook QueryHook) {
db.queryHooks = append(db.queryHooks, hook)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"AddQueryHook",
"(",
"hook",
"QueryHook",
")",
"{",
"db",
".",
"queryHooks",
"=",
"append",
"(",
"db",
".",
"queryHooks",
",",
"hook",
")",
"\n",
"}"
] |
// AddQueryHook adds a hook into query processing.
|
[
"AddQueryHook",
"adds",
"a",
"hook",
"into",
"query",
"processing",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/hook.go#L65-L67
|
train
|
go-pg/pg
|
listener.go
|
Close
|
func (ln *Listener) Close() error {
ln.mu.Lock()
defer ln.mu.Unlock()
if ln.closed {
return errListenerClosed
}
ln.closed = true
close(ln.exit)
return ln._closeTheCn(errListenerClosed)
}
|
go
|
func (ln *Listener) Close() error {
ln.mu.Lock()
defer ln.mu.Unlock()
if ln.closed {
return errListenerClosed
}
ln.closed = true
close(ln.exit)
return ln._closeTheCn(errListenerClosed)
}
|
[
"func",
"(",
"ln",
"*",
"Listener",
")",
"Close",
"(",
")",
"error",
"{",
"ln",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"ln",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"ln",
".",
"closed",
"{",
"return",
"errListenerClosed",
"\n",
"}",
"\n",
"ln",
".",
"closed",
"=",
"true",
"\n",
"close",
"(",
"ln",
".",
"exit",
")",
"\n\n",
"return",
"ln",
".",
"_closeTheCn",
"(",
"errListenerClosed",
")",
"\n",
"}"
] |
// Close closes the listener, releasing any open resources.
|
[
"Close",
"closes",
"the",
"listener",
"releasing",
"any",
"open",
"resources",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/listener.go#L132-L143
|
train
|
go-pg/pg
|
listener.go
|
Listen
|
func (ln *Listener) Listen(channels ...string) error {
// Always append channels so DB.Listen works correctly.
ln.channels = appendIfNotExists(ln.channels, channels...)
cn, err := ln.conn()
if err != nil {
return err
}
err = ln.listen(cn, channels...)
if err != nil {
ln.releaseConn(cn, err, false)
return err
}
return nil
}
|
go
|
func (ln *Listener) Listen(channels ...string) error {
// Always append channels so DB.Listen works correctly.
ln.channels = appendIfNotExists(ln.channels, channels...)
cn, err := ln.conn()
if err != nil {
return err
}
err = ln.listen(cn, channels...)
if err != nil {
ln.releaseConn(cn, err, false)
return err
}
return nil
}
|
[
"func",
"(",
"ln",
"*",
"Listener",
")",
"Listen",
"(",
"channels",
"...",
"string",
")",
"error",
"{",
"// Always append channels so DB.Listen works correctly.",
"ln",
".",
"channels",
"=",
"appendIfNotExists",
"(",
"ln",
".",
"channels",
",",
"channels",
"...",
")",
"\n\n",
"cn",
",",
"err",
":=",
"ln",
".",
"conn",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"ln",
".",
"listen",
"(",
"cn",
",",
"channels",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"ln",
".",
"releaseConn",
"(",
"cn",
",",
"err",
",",
"false",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Listen starts listening for notifications on channels.
|
[
"Listen",
"starts",
"listening",
"for",
"notifications",
"on",
"channels",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/listener.go#L146-L162
|
train
|
go-pg/pg
|
listener.go
|
Receive
|
func (ln *Listener) Receive() (channel string, payload string, err error) {
return ln.ReceiveTimeout(0)
}
|
go
|
func (ln *Listener) Receive() (channel string, payload string, err error) {
return ln.ReceiveTimeout(0)
}
|
[
"func",
"(",
"ln",
"*",
"Listener",
")",
"Receive",
"(",
")",
"(",
"channel",
"string",
",",
"payload",
"string",
",",
"err",
"error",
")",
"{",
"return",
"ln",
".",
"ReceiveTimeout",
"(",
"0",
")",
"\n",
"}"
] |
// Receive indefinitely waits for a notification. This is low-level API
// and in most cases Channel should be used instead.
|
[
"Receive",
"indefinitely",
"waits",
"for",
"a",
"notification",
".",
"This",
"is",
"low",
"-",
"level",
"API",
"and",
"in",
"most",
"cases",
"Channel",
"should",
"be",
"used",
"instead",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/listener.go#L179-L181
|
train
|
go-pg/pg
|
listener.go
|
ReceiveTimeout
|
func (ln *Listener) ReceiveTimeout(timeout time.Duration) (channel, payload string, err error) {
cn, err := ln.conn()
if err != nil {
return "", "", err
}
err = cn.WithReader(timeout, func(rd *internal.BufReader) error {
channel, payload, err = readNotification(rd)
return err
})
if err != nil {
ln.releaseConn(cn, err, timeout > 0)
return "", "", err
}
return channel, payload, nil
}
|
go
|
func (ln *Listener) ReceiveTimeout(timeout time.Duration) (channel, payload string, err error) {
cn, err := ln.conn()
if err != nil {
return "", "", err
}
err = cn.WithReader(timeout, func(rd *internal.BufReader) error {
channel, payload, err = readNotification(rd)
return err
})
if err != nil {
ln.releaseConn(cn, err, timeout > 0)
return "", "", err
}
return channel, payload, nil
}
|
[
"func",
"(",
"ln",
"*",
"Listener",
")",
"ReceiveTimeout",
"(",
"timeout",
"time",
".",
"Duration",
")",
"(",
"channel",
",",
"payload",
"string",
",",
"err",
"error",
")",
"{",
"cn",
",",
"err",
":=",
"ln",
".",
"conn",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"cn",
".",
"WithReader",
"(",
"timeout",
",",
"func",
"(",
"rd",
"*",
"internal",
".",
"BufReader",
")",
"error",
"{",
"channel",
",",
"payload",
",",
"err",
"=",
"readNotification",
"(",
"rd",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"ln",
".",
"releaseConn",
"(",
"cn",
",",
"err",
",",
"timeout",
">",
"0",
")",
"\n",
"return",
"\"",
"\"",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"channel",
",",
"payload",
",",
"nil",
"\n",
"}"
] |
// ReceiveTimeout waits for a notification until timeout is reached.
// This is low-level API and in most cases Channel should be used instead.
|
[
"ReceiveTimeout",
"waits",
"for",
"a",
"notification",
"until",
"timeout",
"is",
"reached",
".",
"This",
"is",
"low",
"-",
"level",
"API",
"and",
"in",
"most",
"cases",
"Channel",
"should",
"be",
"used",
"instead",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/listener.go#L185-L201
|
train
|
go-pg/pg
|
messages.go
|
writeBindExecuteMsg
|
func writeBindExecuteMsg(buf *pool.WriteBuffer, name string, params ...interface{}) error {
buf.StartMessage(bindMsg)
buf.WriteString("")
buf.WriteString(name)
buf.WriteInt16(0)
buf.WriteInt16(int16(len(params)))
for _, param := range params {
buf.StartParam()
bytes := types.Append(buf.Bytes, param, 0)
if bytes != nil {
buf.Bytes = bytes
buf.FinishParam()
} else {
buf.FinishNullParam()
}
}
buf.WriteInt16(0)
buf.FinishMessage()
buf.StartMessage(executeMsg)
buf.WriteString("")
buf.WriteInt32(0)
buf.FinishMessage()
writeSyncMsg(buf)
return nil
}
|
go
|
func writeBindExecuteMsg(buf *pool.WriteBuffer, name string, params ...interface{}) error {
buf.StartMessage(bindMsg)
buf.WriteString("")
buf.WriteString(name)
buf.WriteInt16(0)
buf.WriteInt16(int16(len(params)))
for _, param := range params {
buf.StartParam()
bytes := types.Append(buf.Bytes, param, 0)
if bytes != nil {
buf.Bytes = bytes
buf.FinishParam()
} else {
buf.FinishNullParam()
}
}
buf.WriteInt16(0)
buf.FinishMessage()
buf.StartMessage(executeMsg)
buf.WriteString("")
buf.WriteInt32(0)
buf.FinishMessage()
writeSyncMsg(buf)
return nil
}
|
[
"func",
"writeBindExecuteMsg",
"(",
"buf",
"*",
"pool",
".",
"WriteBuffer",
",",
"name",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"buf",
".",
"StartMessage",
"(",
"bindMsg",
")",
"\n",
"buf",
".",
"WriteString",
"(",
"\"",
"\"",
")",
"\n",
"buf",
".",
"WriteString",
"(",
"name",
")",
"\n",
"buf",
".",
"WriteInt16",
"(",
"0",
")",
"\n",
"buf",
".",
"WriteInt16",
"(",
"int16",
"(",
"len",
"(",
"params",
")",
")",
")",
"\n",
"for",
"_",
",",
"param",
":=",
"range",
"params",
"{",
"buf",
".",
"StartParam",
"(",
")",
"\n",
"bytes",
":=",
"types",
".",
"Append",
"(",
"buf",
".",
"Bytes",
",",
"param",
",",
"0",
")",
"\n",
"if",
"bytes",
"!=",
"nil",
"{",
"buf",
".",
"Bytes",
"=",
"bytes",
"\n",
"buf",
".",
"FinishParam",
"(",
")",
"\n",
"}",
"else",
"{",
"buf",
".",
"FinishNullParam",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"buf",
".",
"WriteInt16",
"(",
"0",
")",
"\n",
"buf",
".",
"FinishMessage",
"(",
")",
"\n\n",
"buf",
".",
"StartMessage",
"(",
"executeMsg",
")",
"\n",
"buf",
".",
"WriteString",
"(",
"\"",
"\"",
")",
"\n",
"buf",
".",
"WriteInt32",
"(",
"0",
")",
"\n",
"buf",
".",
"FinishMessage",
"(",
")",
"\n\n",
"writeSyncMsg",
"(",
"buf",
")",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Writes BIND, EXECUTE and SYNC messages.
|
[
"Writes",
"BIND",
"EXECUTE",
"and",
"SYNC",
"messages",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/messages.go#L535-L562
|
train
|
go-pg/pg
|
stmt.go
|
Exec
|
func (stmt *Stmt) Exec(params ...interface{}) (Result, error) {
return stmt.exec(context.TODO(), params...)
}
|
go
|
func (stmt *Stmt) Exec(params ...interface{}) (Result, error) {
return stmt.exec(context.TODO(), params...)
}
|
[
"func",
"(",
"stmt",
"*",
"Stmt",
")",
"Exec",
"(",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"stmt",
".",
"exec",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"params",
"...",
")",
"\n",
"}"
] |
// Exec executes a prepared statement with the given parameters.
|
[
"Exec",
"executes",
"a",
"prepared",
"statement",
"with",
"the",
"given",
"parameters",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/stmt.go#L78-L80
|
train
|
go-pg/pg
|
stmt.go
|
ExecContext
|
func (stmt *Stmt) ExecContext(c context.Context, params ...interface{}) (Result, error) {
return stmt.exec(c, params...)
}
|
go
|
func (stmt *Stmt) ExecContext(c context.Context, params ...interface{}) (Result, error) {
return stmt.exec(c, params...)
}
|
[
"func",
"(",
"stmt",
"*",
"Stmt",
")",
"ExecContext",
"(",
"c",
"context",
".",
"Context",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"stmt",
".",
"exec",
"(",
"c",
",",
"params",
"...",
")",
"\n",
"}"
] |
// ExecContext executes a prepared statement with the given parameters.
|
[
"ExecContext",
"executes",
"a",
"prepared",
"statement",
"with",
"the",
"given",
"parameters",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/stmt.go#L83-L85
|
train
|
go-pg/pg
|
stmt.go
|
Query
|
func (stmt *Stmt) Query(model interface{}, params ...interface{}) (Result, error) {
return stmt.query(context.TODO(), model, params...)
}
|
go
|
func (stmt *Stmt) Query(model interface{}, params ...interface{}) (Result, error) {
return stmt.query(context.TODO(), model, params...)
}
|
[
"func",
"(",
"stmt",
"*",
"Stmt",
")",
"Query",
"(",
"model",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"stmt",
".",
"query",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"model",
",",
"params",
"...",
")",
"\n",
"}"
] |
// Query executes a prepared query statement with the given parameters.
|
[
"Query",
"executes",
"a",
"prepared",
"query",
"statement",
"with",
"the",
"given",
"parameters",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/stmt.go#L135-L137
|
train
|
go-pg/pg
|
base.go
|
Exec
|
func (db *baseDB) Exec(query interface{}, params ...interface{}) (res Result, err error) {
return db.exec(context.TODO(), query, params...)
}
|
go
|
func (db *baseDB) Exec(query interface{}, params ...interface{}) (res Result, err error) {
return db.exec(context.TODO(), query, params...)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Exec",
"(",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"res",
"Result",
",",
"err",
"error",
")",
"{",
"return",
"db",
".",
"exec",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// Exec executes a query ignoring returned rows. The params are for any
// placeholders in the query.
|
[
"Exec",
"executes",
"a",
"query",
"ignoring",
"returned",
"rows",
".",
"The",
"params",
"are",
"for",
"any",
"placeholders",
"in",
"the",
"query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L186-L188
|
train
|
go-pg/pg
|
base.go
|
Query
|
func (db *baseDB) Query(model, query interface{}, params ...interface{}) (res Result, err error) {
return db.query(context.TODO(), model, query, params...)
}
|
go
|
func (db *baseDB) Query(model, query interface{}, params ...interface{}) (res Result, err error) {
return db.query(context.TODO(), model, query, params...)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Query",
"(",
"model",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"res",
"Result",
",",
"err",
"error",
")",
"{",
"return",
"db",
".",
"query",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"model",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// Query executes a query that returns rows, typically a SELECT.
// The params are for any placeholders in the query.
|
[
"Query",
"executes",
"a",
"query",
"that",
"returns",
"rows",
"typically",
"a",
"SELECT",
".",
"The",
"params",
"are",
"for",
"any",
"placeholders",
"in",
"the",
"query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L239-L241
|
train
|
go-pg/pg
|
base.go
|
Model
|
func (db *baseDB) Model(model ...interface{}) *orm.Query {
return orm.NewQuery(db.db, model...)
}
|
go
|
func (db *baseDB) Model(model ...interface{}) *orm.Query {
return orm.NewQuery(db.db, model...)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Model",
"(",
"model",
"...",
"interface",
"{",
"}",
")",
"*",
"orm",
".",
"Query",
"{",
"return",
"orm",
".",
"NewQuery",
"(",
"db",
".",
"db",
",",
"model",
"...",
")",
"\n",
"}"
] |
// Model returns new query for the model.
|
[
"Model",
"returns",
"new",
"query",
"for",
"the",
"model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L389-L391
|
train
|
go-pg/pg
|
base.go
|
Select
|
func (db *baseDB) Select(model interface{}) error {
return orm.Select(db.db, model)
}
|
go
|
func (db *baseDB) Select(model interface{}) error {
return orm.Select(db.db, model)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Select",
"(",
"model",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"orm",
".",
"Select",
"(",
"db",
".",
"db",
",",
"model",
")",
"\n",
"}"
] |
// Select selects the model by primary key.
|
[
"Select",
"selects",
"the",
"model",
"by",
"primary",
"key",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L398-L400
|
train
|
go-pg/pg
|
base.go
|
Insert
|
func (db *baseDB) Insert(model ...interface{}) error {
return orm.Insert(db.db, model...)
}
|
go
|
func (db *baseDB) Insert(model ...interface{}) error {
return orm.Insert(db.db, model...)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Insert",
"(",
"model",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"orm",
".",
"Insert",
"(",
"db",
".",
"db",
",",
"model",
"...",
")",
"\n",
"}"
] |
// Insert inserts the model updating primary keys if they are empty.
|
[
"Insert",
"inserts",
"the",
"model",
"updating",
"primary",
"keys",
"if",
"they",
"are",
"empty",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L403-L405
|
train
|
go-pg/pg
|
base.go
|
Update
|
func (db *baseDB) Update(model interface{}) error {
return orm.Update(db.db, model)
}
|
go
|
func (db *baseDB) Update(model interface{}) error {
return orm.Update(db.db, model)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Update",
"(",
"model",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"orm",
".",
"Update",
"(",
"db",
".",
"db",
",",
"model",
")",
"\n",
"}"
] |
// Update updates the model by primary key.
|
[
"Update",
"updates",
"the",
"model",
"by",
"primary",
"key",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L408-L410
|
train
|
go-pg/pg
|
base.go
|
Delete
|
func (db *baseDB) Delete(model interface{}) error {
return orm.Delete(db.db, model)
}
|
go
|
func (db *baseDB) Delete(model interface{}) error {
return orm.Delete(db.db, model)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"Delete",
"(",
"model",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"orm",
".",
"Delete",
"(",
"db",
".",
"db",
",",
"model",
")",
"\n",
"}"
] |
// Delete deletes the model by primary key.
|
[
"Delete",
"deletes",
"the",
"model",
"by",
"primary",
"key",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L413-L415
|
train
|
go-pg/pg
|
base.go
|
DropTable
|
func (db *baseDB) DropTable(model interface{}, opt *orm.DropTableOptions) error {
return orm.DropTable(db.db, model, opt)
}
|
go
|
func (db *baseDB) DropTable(model interface{}, opt *orm.DropTableOptions) error {
return orm.DropTable(db.db, model, opt)
}
|
[
"func",
"(",
"db",
"*",
"baseDB",
")",
"DropTable",
"(",
"model",
"interface",
"{",
"}",
",",
"opt",
"*",
"orm",
".",
"DropTableOptions",
")",
"error",
"{",
"return",
"orm",
".",
"DropTable",
"(",
"db",
".",
"db",
",",
"model",
",",
"opt",
")",
"\n",
"}"
] |
// DropTable drops table for the model.
|
[
"DropTable",
"drops",
"table",
"for",
"the",
"model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/base.go#L431-L433
|
train
|
go-pg/pg
|
pg.go
|
ModelContext
|
func ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, nil, model...)
}
|
go
|
func ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, nil, model...)
}
|
[
"func",
"ModelContext",
"(",
"c",
"context",
".",
"Context",
",",
"model",
"...",
"interface",
"{",
"}",
")",
"*",
"orm",
".",
"Query",
"{",
"return",
"orm",
".",
"NewQueryContext",
"(",
"c",
",",
"nil",
",",
"model",
"...",
")",
"\n",
"}"
] |
// ModelContext returns a new query for the optional model with a context
|
[
"ModelContext",
"returns",
"a",
"new",
"query",
"for",
"the",
"optional",
"model",
"with",
"a",
"context"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L33-L35
|
train
|
go-pg/pg
|
pg.go
|
Q
|
func Q(query string, params ...interface{}) types.ValueAppender {
return orm.Q(query, params...)
}
|
go
|
func Q(query string, params ...interface{}) types.ValueAppender {
return orm.Q(query, params...)
}
|
[
"func",
"Q",
"(",
"query",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"types",
".",
"ValueAppender",
"{",
"return",
"orm",
".",
"Q",
"(",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// Q replaces any placeholders found in the query.
|
[
"Q",
"replaces",
"any",
"placeholders",
"found",
"in",
"the",
"query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L44-L46
|
train
|
go-pg/pg
|
pg.go
|
Init
|
func (strings *Strings) Init() error {
if s := *strings; len(s) > 0 {
*strings = s[:0]
}
return nil
}
|
go
|
func (strings *Strings) Init() error {
if s := *strings; len(s) > 0 {
*strings = s[:0]
}
return nil
}
|
[
"func",
"(",
"strings",
"*",
"Strings",
")",
"Init",
"(",
")",
"error",
"{",
"if",
"s",
":=",
"*",
"strings",
";",
"len",
"(",
"s",
")",
">",
"0",
"{",
"*",
"strings",
"=",
"s",
"[",
":",
"0",
"]",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Init initializes the Strings slice
|
[
"Init",
"initializes",
"the",
"Strings",
"slice"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L113-L118
|
train
|
go-pg/pg
|
pg.go
|
ScanColumn
|
func (strings *Strings) ScanColumn(colIdx int, _ string, rd types.Reader, n int) error {
b := make([]byte, n)
_, err := io.ReadFull(rd, b)
if err != nil {
return err
}
*strings = append(*strings, internal.BytesToString(b))
return nil
}
|
go
|
func (strings *Strings) ScanColumn(colIdx int, _ string, rd types.Reader, n int) error {
b := make([]byte, n)
_, err := io.ReadFull(rd, b)
if err != nil {
return err
}
*strings = append(*strings, internal.BytesToString(b))
return nil
}
|
[
"func",
"(",
"strings",
"*",
"Strings",
")",
"ScanColumn",
"(",
"colIdx",
"int",
",",
"_",
"string",
",",
"rd",
"types",
".",
"Reader",
",",
"n",
"int",
")",
"error",
"{",
"b",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"n",
")",
"\n",
"_",
",",
"err",
":=",
"io",
".",
"ReadFull",
"(",
"rd",
",",
"b",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"*",
"strings",
"=",
"append",
"(",
"*",
"strings",
",",
"internal",
".",
"BytesToString",
"(",
"b",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// ScanColumn scans the columns and appends them to `strings`
|
[
"ScanColumn",
"scans",
"the",
"columns",
"and",
"appends",
"them",
"to",
"strings"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L131-L140
|
train
|
go-pg/pg
|
pg.go
|
AppendValue
|
func (strings Strings) AppendValue(dst []byte, quote int) []byte {
if len(strings) == 0 {
return dst
}
for _, s := range strings {
dst = types.AppendString(dst, s, 1)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
|
go
|
func (strings Strings) AppendValue(dst []byte, quote int) []byte {
if len(strings) == 0 {
return dst
}
for _, s := range strings {
dst = types.AppendString(dst, s, 1)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
|
[
"func",
"(",
"strings",
"Strings",
")",
"AppendValue",
"(",
"dst",
"[",
"]",
"byte",
",",
"quote",
"int",
")",
"[",
"]",
"byte",
"{",
"if",
"len",
"(",
"strings",
")",
"==",
"0",
"{",
"return",
"dst",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"s",
":=",
"range",
"strings",
"{",
"dst",
"=",
"types",
".",
"AppendString",
"(",
"dst",
",",
"s",
",",
"1",
")",
"\n",
"dst",
"=",
"append",
"(",
"dst",
",",
"','",
")",
"\n",
"}",
"\n",
"dst",
"=",
"dst",
"[",
":",
"len",
"(",
"dst",
")",
"-",
"1",
"]",
"\n",
"return",
"dst",
"\n",
"}"
] |
// AppendValue appends the values from `strings` to the given byte slice
|
[
"AppendValue",
"appends",
"the",
"values",
"from",
"strings",
"to",
"the",
"given",
"byte",
"slice"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L143-L154
|
train
|
go-pg/pg
|
pg.go
|
Init
|
func (ints *Ints) Init() error {
if s := *ints; len(s) > 0 {
*ints = s[:0]
}
return nil
}
|
go
|
func (ints *Ints) Init() error {
if s := *ints; len(s) > 0 {
*ints = s[:0]
}
return nil
}
|
[
"func",
"(",
"ints",
"*",
"Ints",
")",
"Init",
"(",
")",
"error",
"{",
"if",
"s",
":=",
"*",
"ints",
";",
"len",
"(",
"s",
")",
">",
"0",
"{",
"*",
"ints",
"=",
"s",
"[",
":",
"0",
"]",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Init initializes the Int slice
|
[
"Init",
"initializes",
"the",
"Int",
"slice"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L165-L170
|
train
|
go-pg/pg
|
pg.go
|
ScanColumn
|
func (ints *Ints) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
*ints = append(*ints, num)
return nil
}
|
go
|
func (ints *Ints) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
*ints = append(*ints, num)
return nil
}
|
[
"func",
"(",
"ints",
"*",
"Ints",
")",
"ScanColumn",
"(",
"colIdx",
"int",
",",
"colName",
"string",
",",
"rd",
"types",
".",
"Reader",
",",
"n",
"int",
")",
"error",
"{",
"num",
",",
"err",
":=",
"types",
".",
"ScanInt64",
"(",
"rd",
",",
"n",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"*",
"ints",
"=",
"append",
"(",
"*",
"ints",
",",
"num",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// ScanColumn scans the columns and appends them to `ints`
|
[
"ScanColumn",
"scans",
"the",
"columns",
"and",
"appends",
"them",
"to",
"ints"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L183-L191
|
train
|
go-pg/pg
|
pg.go
|
AppendValue
|
func (ints Ints) AppendValue(dst []byte, quote int) []byte {
if len(ints) == 0 {
return dst
}
for _, v := range ints {
dst = strconv.AppendInt(dst, v, 10)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
|
go
|
func (ints Ints) AppendValue(dst []byte, quote int) []byte {
if len(ints) == 0 {
return dst
}
for _, v := range ints {
dst = strconv.AppendInt(dst, v, 10)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
|
[
"func",
"(",
"ints",
"Ints",
")",
"AppendValue",
"(",
"dst",
"[",
"]",
"byte",
",",
"quote",
"int",
")",
"[",
"]",
"byte",
"{",
"if",
"len",
"(",
"ints",
")",
"==",
"0",
"{",
"return",
"dst",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"v",
":=",
"range",
"ints",
"{",
"dst",
"=",
"strconv",
".",
"AppendInt",
"(",
"dst",
",",
"v",
",",
"10",
")",
"\n",
"dst",
"=",
"append",
"(",
"dst",
",",
"','",
")",
"\n",
"}",
"\n",
"dst",
"=",
"dst",
"[",
":",
"len",
"(",
"dst",
")",
"-",
"1",
"]",
"\n",
"return",
"dst",
"\n",
"}"
] |
// AppendValue appends the values from `ints` to the given byte slice
|
[
"AppendValue",
"appends",
"the",
"values",
"from",
"ints",
"to",
"the",
"given",
"byte",
"slice"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L194-L205
|
train
|
go-pg/pg
|
pg.go
|
Init
|
func (set *IntSet) Init() error {
if len(*set) > 0 {
*set = make(map[int64]struct{})
}
return nil
}
|
go
|
func (set *IntSet) Init() error {
if len(*set) > 0 {
*set = make(map[int64]struct{})
}
return nil
}
|
[
"func",
"(",
"set",
"*",
"IntSet",
")",
"Init",
"(",
")",
"error",
"{",
"if",
"len",
"(",
"*",
"set",
")",
">",
"0",
"{",
"*",
"set",
"=",
"make",
"(",
"map",
"[",
"int64",
"]",
"struct",
"{",
"}",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Init initializes the IntSet
|
[
"Init",
"initializes",
"the",
"IntSet"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L215-L220
|
train
|
go-pg/pg
|
pg.go
|
ScanColumn
|
func (set *IntSet) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
setVal := *set
if setVal == nil {
*set = make(IntSet)
setVal = *set
}
setVal[num] = struct{}{}
return nil
}
|
go
|
func (set *IntSet) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
setVal := *set
if setVal == nil {
*set = make(IntSet)
setVal = *set
}
setVal[num] = struct{}{}
return nil
}
|
[
"func",
"(",
"set",
"*",
"IntSet",
")",
"ScanColumn",
"(",
"colIdx",
"int",
",",
"colName",
"string",
",",
"rd",
"types",
".",
"Reader",
",",
"n",
"int",
")",
"error",
"{",
"num",
",",
"err",
":=",
"types",
".",
"ScanInt64",
"(",
"rd",
",",
"n",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"setVal",
":=",
"*",
"set",
"\n",
"if",
"setVal",
"==",
"nil",
"{",
"*",
"set",
"=",
"make",
"(",
"IntSet",
")",
"\n",
"setVal",
"=",
"*",
"set",
"\n",
"}",
"\n\n",
"setVal",
"[",
"num",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// ScanColumn scans the columns and appends them to `IntSet`
|
[
"ScanColumn",
"scans",
"the",
"columns",
"and",
"appends",
"them",
"to",
"IntSet"
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/pg.go#L233-L247
|
train
|
go-pg/pg
|
internal/buf_reader.go
|
Buffered
|
func (b *BufReader) Buffered() int {
d := b.w - b.r
if b.available != -1 && d > b.available {
return b.available
}
return d
}
|
go
|
func (b *BufReader) Buffered() int {
d := b.w - b.r
if b.available != -1 && d > b.available {
return b.available
}
return d
}
|
[
"func",
"(",
"b",
"*",
"BufReader",
")",
"Buffered",
"(",
")",
"int",
"{",
"d",
":=",
"b",
".",
"w",
"-",
"b",
".",
"r",
"\n",
"if",
"b",
".",
"available",
"!=",
"-",
"1",
"&&",
"d",
">",
"b",
".",
"available",
"{",
"return",
"b",
".",
"available",
"\n",
"}",
"\n",
"return",
"d",
"\n",
"}"
] |
// Buffered returns the number of bytes that can be read from the current buffer.
|
[
"Buffered",
"returns",
"the",
"number",
"of",
"bytes",
"that",
"can",
"be",
"read",
"from",
"the",
"current",
"buffer",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/internal/buf_reader.go#L68-L74
|
train
|
go-pg/pg
|
orm/query.go
|
New
|
func (q *Query) New() *Query {
cp := &Query{
ctx: q.ctx,
db: q.db,
model: q.model,
implicitModel: true,
deleted: q.deleted,
}
return cp
}
|
go
|
func (q *Query) New() *Query {
cp := &Query{
ctx: q.ctx,
db: q.db,
model: q.model,
implicitModel: true,
deleted: q.deleted,
}
return cp
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"New",
"(",
")",
"*",
"Query",
"{",
"cp",
":=",
"&",
"Query",
"{",
"ctx",
":",
"q",
".",
"ctx",
",",
"db",
":",
"q",
".",
"db",
",",
"model",
":",
"q",
".",
"model",
",",
"implicitModel",
":",
"true",
",",
"deleted",
":",
"q",
".",
"deleted",
",",
"}",
"\n",
"return",
"cp",
"\n",
"}"
] |
// New returns new zero Query binded to the current db.
|
[
"New",
"returns",
"new",
"zero",
"Query",
"binded",
"to",
"the",
"current",
"db",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L68-L77
|
train
|
go-pg/pg
|
orm/query.go
|
Copy
|
func (q *Query) Copy() *Query {
var modelValues map[string]*queryParamsAppender
if len(q.modelValues) > 0 {
modelValues = make(map[string]*queryParamsAppender, len(q.modelValues))
for k, v := range q.modelValues {
modelValues[k] = v
}
}
copy := &Query{
ctx: q.ctx,
db: q.db,
stickyErr: q.stickyErr,
model: q.model,
implicitModel: q.implicitModel,
deleted: q.deleted,
with: q.with[:len(q.with):len(q.with)],
tables: q.tables[:len(q.tables):len(q.tables)],
columns: q.columns[:len(q.columns):len(q.columns)],
set: q.set[:len(q.set):len(q.set)],
modelValues: modelValues,
where: q.where[:len(q.where):len(q.where)],
updWhere: q.updWhere[:len(q.updWhere):len(q.updWhere)],
joins: q.joins[:len(q.joins):len(q.joins)],
group: q.group[:len(q.group):len(q.group)],
having: q.having[:len(q.having):len(q.having)],
order: q.order[:len(q.order):len(q.order)],
onConflict: q.onConflict,
returning: q.returning[:len(q.returning):len(q.returning)],
limit: q.limit,
offset: q.offset,
selFor: q.selFor,
}
return copy
}
|
go
|
func (q *Query) Copy() *Query {
var modelValues map[string]*queryParamsAppender
if len(q.modelValues) > 0 {
modelValues = make(map[string]*queryParamsAppender, len(q.modelValues))
for k, v := range q.modelValues {
modelValues[k] = v
}
}
copy := &Query{
ctx: q.ctx,
db: q.db,
stickyErr: q.stickyErr,
model: q.model,
implicitModel: q.implicitModel,
deleted: q.deleted,
with: q.with[:len(q.with):len(q.with)],
tables: q.tables[:len(q.tables):len(q.tables)],
columns: q.columns[:len(q.columns):len(q.columns)],
set: q.set[:len(q.set):len(q.set)],
modelValues: modelValues,
where: q.where[:len(q.where):len(q.where)],
updWhere: q.updWhere[:len(q.updWhere):len(q.updWhere)],
joins: q.joins[:len(q.joins):len(q.joins)],
group: q.group[:len(q.group):len(q.group)],
having: q.having[:len(q.having):len(q.having)],
order: q.order[:len(q.order):len(q.order)],
onConflict: q.onConflict,
returning: q.returning[:len(q.returning):len(q.returning)],
limit: q.limit,
offset: q.offset,
selFor: q.selFor,
}
return copy
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Copy",
"(",
")",
"*",
"Query",
"{",
"var",
"modelValues",
"map",
"[",
"string",
"]",
"*",
"queryParamsAppender",
"\n",
"if",
"len",
"(",
"q",
".",
"modelValues",
")",
">",
"0",
"{",
"modelValues",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"queryParamsAppender",
",",
"len",
"(",
"q",
".",
"modelValues",
")",
")",
"\n",
"for",
"k",
",",
"v",
":=",
"range",
"q",
".",
"modelValues",
"{",
"modelValues",
"[",
"k",
"]",
"=",
"v",
"\n",
"}",
"\n",
"}",
"\n\n",
"copy",
":=",
"&",
"Query",
"{",
"ctx",
":",
"q",
".",
"ctx",
",",
"db",
":",
"q",
".",
"db",
",",
"stickyErr",
":",
"q",
".",
"stickyErr",
",",
"model",
":",
"q",
".",
"model",
",",
"implicitModel",
":",
"q",
".",
"implicitModel",
",",
"deleted",
":",
"q",
".",
"deleted",
",",
"with",
":",
"q",
".",
"with",
"[",
":",
"len",
"(",
"q",
".",
"with",
")",
":",
"len",
"(",
"q",
".",
"with",
")",
"]",
",",
"tables",
":",
"q",
".",
"tables",
"[",
":",
"len",
"(",
"q",
".",
"tables",
")",
":",
"len",
"(",
"q",
".",
"tables",
")",
"]",
",",
"columns",
":",
"q",
".",
"columns",
"[",
":",
"len",
"(",
"q",
".",
"columns",
")",
":",
"len",
"(",
"q",
".",
"columns",
")",
"]",
",",
"set",
":",
"q",
".",
"set",
"[",
":",
"len",
"(",
"q",
".",
"set",
")",
":",
"len",
"(",
"q",
".",
"set",
")",
"]",
",",
"modelValues",
":",
"modelValues",
",",
"where",
":",
"q",
".",
"where",
"[",
":",
"len",
"(",
"q",
".",
"where",
")",
":",
"len",
"(",
"q",
".",
"where",
")",
"]",
",",
"updWhere",
":",
"q",
".",
"updWhere",
"[",
":",
"len",
"(",
"q",
".",
"updWhere",
")",
":",
"len",
"(",
"q",
".",
"updWhere",
")",
"]",
",",
"joins",
":",
"q",
".",
"joins",
"[",
":",
"len",
"(",
"q",
".",
"joins",
")",
":",
"len",
"(",
"q",
".",
"joins",
")",
"]",
",",
"group",
":",
"q",
".",
"group",
"[",
":",
"len",
"(",
"q",
".",
"group",
")",
":",
"len",
"(",
"q",
".",
"group",
")",
"]",
",",
"having",
":",
"q",
".",
"having",
"[",
":",
"len",
"(",
"q",
".",
"having",
")",
":",
"len",
"(",
"q",
".",
"having",
")",
"]",
",",
"order",
":",
"q",
".",
"order",
"[",
":",
"len",
"(",
"q",
".",
"order",
")",
":",
"len",
"(",
"q",
".",
"order",
")",
"]",
",",
"onConflict",
":",
"q",
".",
"onConflict",
",",
"returning",
":",
"q",
".",
"returning",
"[",
":",
"len",
"(",
"q",
".",
"returning",
")",
":",
"len",
"(",
"q",
".",
"returning",
")",
"]",
",",
"limit",
":",
"q",
".",
"limit",
",",
"offset",
":",
"q",
".",
"offset",
",",
"selFor",
":",
"q",
".",
"selFor",
",",
"}",
"\n\n",
"return",
"copy",
"\n",
"}"
] |
// Copy returns copy of the Query.
|
[
"Copy",
"returns",
"copy",
"of",
"the",
"Query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L80-L117
|
train
|
go-pg/pg
|
orm/query.go
|
Deleted
|
func (q *Query) Deleted() *Query {
if q.model != nil {
if err := q.model.Table().mustSoftDelete(); err != nil {
return q.err(err)
}
}
q.deleted = true
return q
}
|
go
|
func (q *Query) Deleted() *Query {
if q.model != nil {
if err := q.model.Table().mustSoftDelete(); err != nil {
return q.err(err)
}
}
q.deleted = true
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Deleted",
"(",
")",
"*",
"Query",
"{",
"if",
"q",
".",
"model",
"!=",
"nil",
"{",
"if",
"err",
":=",
"q",
".",
"model",
".",
"Table",
"(",
")",
".",
"mustSoftDelete",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"q",
".",
"err",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"q",
".",
"deleted",
"=",
"true",
"\n",
"return",
"q",
"\n",
"}"
] |
// Deleted adds `WHERE deleted_at IS NOT NULL` clause for soft deleted models.
|
[
"Deleted",
"adds",
"WHERE",
"deleted_at",
"IS",
"NOT",
"NULL",
"clause",
"for",
"soft",
"deleted",
"models",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L165-L173
|
train
|
go-pg/pg
|
orm/query.go
|
With
|
func (q *Query) With(name string, subq *Query) *Query {
return q._with(name, newSelectQuery(subq))
}
|
go
|
func (q *Query) With(name string, subq *Query) *Query {
return q._with(name, newSelectQuery(subq))
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"With",
"(",
"name",
"string",
",",
"subq",
"*",
"Query",
")",
"*",
"Query",
"{",
"return",
"q",
".",
"_with",
"(",
"name",
",",
"newSelectQuery",
"(",
"subq",
")",
")",
"\n",
"}"
] |
// With adds subq as common table expression with the given name.
|
[
"With",
"adds",
"subq",
"as",
"common",
"table",
"expression",
"with",
"the",
"given",
"name",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L176-L178
|
train
|
go-pg/pg
|
orm/query.go
|
WrapWith
|
func (q *Query) WrapWith(name string) *Query {
wrapper := q.New()
wrapper.with = q.with
q.with = nil
wrapper = wrapper.With(name, q)
return wrapper
}
|
go
|
func (q *Query) WrapWith(name string) *Query {
wrapper := q.New()
wrapper.with = q.with
q.with = nil
wrapper = wrapper.With(name, q)
return wrapper
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"WrapWith",
"(",
"name",
"string",
")",
"*",
"Query",
"{",
"wrapper",
":=",
"q",
".",
"New",
"(",
")",
"\n",
"wrapper",
".",
"with",
"=",
"q",
".",
"with",
"\n",
"q",
".",
"with",
"=",
"nil",
"\n",
"wrapper",
"=",
"wrapper",
".",
"With",
"(",
"name",
",",
"q",
")",
"\n",
"return",
"wrapper",
"\n",
"}"
] |
// WrapWith creates new Query and adds to it current query as
// common table expression with the given name.
|
[
"WrapWith",
"creates",
"new",
"Query",
"and",
"adds",
"to",
"it",
"current",
"query",
"as",
"common",
"table",
"expression",
"with",
"the",
"given",
"name",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L194-L200
|
train
|
go-pg/pg
|
orm/query.go
|
ColumnExpr
|
func (q *Query) ColumnExpr(expr string, params ...interface{}) *Query {
q.columns = append(q.columns, &queryParamsAppender{expr, params})
return q
}
|
go
|
func (q *Query) ColumnExpr(expr string, params ...interface{}) *Query {
q.columns = append(q.columns, &queryParamsAppender{expr, params})
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"ColumnExpr",
"(",
"expr",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"*",
"Query",
"{",
"q",
".",
"columns",
"=",
"append",
"(",
"q",
".",
"columns",
",",
"&",
"queryParamsAppender",
"{",
"expr",
",",
"params",
"}",
")",
"\n",
"return",
"q",
"\n",
"}"
] |
// ColumnExpr adds column expression to the Query.
|
[
"ColumnExpr",
"adds",
"column",
"expression",
"to",
"the",
"Query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L242-L245
|
train
|
go-pg/pg
|
orm/query.go
|
ExcludeColumn
|
func (q *Query) ExcludeColumn(columns ...string) *Query {
if q.columns == nil {
for _, f := range q.model.Table().Fields {
q.columns = append(q.columns, fieldAppender{f.SQLName})
}
}
for _, col := range columns {
if !q.excludeColumn(col) {
return q.err(fmt.Errorf("pg: can't find column=%q", col))
}
}
return q
}
|
go
|
func (q *Query) ExcludeColumn(columns ...string) *Query {
if q.columns == nil {
for _, f := range q.model.Table().Fields {
q.columns = append(q.columns, fieldAppender{f.SQLName})
}
}
for _, col := range columns {
if !q.excludeColumn(col) {
return q.err(fmt.Errorf("pg: can't find column=%q", col))
}
}
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"ExcludeColumn",
"(",
"columns",
"...",
"string",
")",
"*",
"Query",
"{",
"if",
"q",
".",
"columns",
"==",
"nil",
"{",
"for",
"_",
",",
"f",
":=",
"range",
"q",
".",
"model",
".",
"Table",
"(",
")",
".",
"Fields",
"{",
"q",
".",
"columns",
"=",
"append",
"(",
"q",
".",
"columns",
",",
"fieldAppender",
"{",
"f",
".",
"SQLName",
"}",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"col",
":=",
"range",
"columns",
"{",
"if",
"!",
"q",
".",
"excludeColumn",
"(",
"col",
")",
"{",
"return",
"q",
".",
"err",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"col",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"q",
"\n",
"}"
] |
// ExcludeColumn excludes a column from the list of to be selected columns.
|
[
"ExcludeColumn",
"excludes",
"a",
"column",
"from",
"the",
"list",
"of",
"to",
"be",
"selected",
"columns",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L248-L261
|
train
|
go-pg/pg
|
orm/query.go
|
Value
|
func (q *Query) Value(column string, value string, params ...interface{}) *Query {
if !q.hasModel() {
q.err(errModelNil)
return q
}
table := q.model.Table()
if _, ok := table.FieldsMap[column]; !ok {
q.err(fmt.Errorf("%s does not have column=%q", table, column))
return q
}
if q.modelValues == nil {
q.modelValues = make(map[string]*queryParamsAppender)
}
q.modelValues[column] = &queryParamsAppender{value, params}
return q
}
|
go
|
func (q *Query) Value(column string, value string, params ...interface{}) *Query {
if !q.hasModel() {
q.err(errModelNil)
return q
}
table := q.model.Table()
if _, ok := table.FieldsMap[column]; !ok {
q.err(fmt.Errorf("%s does not have column=%q", table, column))
return q
}
if q.modelValues == nil {
q.modelValues = make(map[string]*queryParamsAppender)
}
q.modelValues[column] = &queryParamsAppender{value, params}
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Value",
"(",
"column",
"string",
",",
"value",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"*",
"Query",
"{",
"if",
"!",
"q",
".",
"hasModel",
"(",
")",
"{",
"q",
".",
"err",
"(",
"errModelNil",
")",
"\n",
"return",
"q",
"\n",
"}",
"\n\n",
"table",
":=",
"q",
".",
"model",
".",
"Table",
"(",
")",
"\n",
"if",
"_",
",",
"ok",
":=",
"table",
".",
"FieldsMap",
"[",
"column",
"]",
";",
"!",
"ok",
"{",
"q",
".",
"err",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"table",
",",
"column",
")",
")",
"\n",
"return",
"q",
"\n",
"}",
"\n\n",
"if",
"q",
".",
"modelValues",
"==",
"nil",
"{",
"q",
".",
"modelValues",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"queryParamsAppender",
")",
"\n",
"}",
"\n",
"q",
".",
"modelValues",
"[",
"column",
"]",
"=",
"&",
"queryParamsAppender",
"{",
"value",
",",
"params",
"}",
"\n",
"return",
"q",
"\n",
"}"
] |
// Value overwrites model value for the column in INSERT and UPDATE queries.
|
[
"Value",
"overwrites",
"model",
"value",
"for",
"the",
"column",
"in",
"INSERT",
"and",
"UPDATE",
"queries",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L345-L362
|
train
|
go-pg/pg
|
orm/query.go
|
JoinOn
|
func (q *Query) JoinOn(condition string, params ...interface{}) *Query {
if q.joinAppendOn == nil {
q.err(errors.New("pg: no joins to apply JoinOn"))
return q
}
q.joinAppendOn(&condAppender{
sep: " AND ",
cond: condition,
params: params,
})
return q
}
|
go
|
func (q *Query) JoinOn(condition string, params ...interface{}) *Query {
if q.joinAppendOn == nil {
q.err(errors.New("pg: no joins to apply JoinOn"))
return q
}
q.joinAppendOn(&condAppender{
sep: " AND ",
cond: condition,
params: params,
})
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"JoinOn",
"(",
"condition",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"*",
"Query",
"{",
"if",
"q",
".",
"joinAppendOn",
"==",
"nil",
"{",
"q",
".",
"err",
"(",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"return",
"q",
"\n",
"}",
"\n",
"q",
".",
"joinAppendOn",
"(",
"&",
"condAppender",
"{",
"sep",
":",
"\"",
"\"",
",",
"cond",
":",
"condition",
",",
"params",
":",
"params",
",",
"}",
")",
"\n",
"return",
"q",
"\n",
"}"
] |
// JoinOn appends join condition to the last join.
|
[
"JoinOn",
"appends",
"join",
"condition",
"to",
"the",
"last",
"join",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L506-L517
|
train
|
go-pg/pg
|
orm/query.go
|
Order
|
func (q *Query) Order(orders ...string) *Query {
loop:
for _, order := range orders {
if order == "" {
continue
}
ind := strings.Index(order, " ")
if ind != -1 {
field := order[:ind]
sort := order[ind+1:]
switch internal.UpperString(sort) {
case "ASC", "DESC", "ASC NULLS FIRST", "DESC NULLS FIRST",
"ASC NULLS LAST", "DESC NULLS LAST":
q = q.OrderExpr("? ?", types.F(field), types.Q(sort))
continue loop
}
}
q.order = append(q.order, fieldAppender{order})
}
return q
}
|
go
|
func (q *Query) Order(orders ...string) *Query {
loop:
for _, order := range orders {
if order == "" {
continue
}
ind := strings.Index(order, " ")
if ind != -1 {
field := order[:ind]
sort := order[ind+1:]
switch internal.UpperString(sort) {
case "ASC", "DESC", "ASC NULLS FIRST", "DESC NULLS FIRST",
"ASC NULLS LAST", "DESC NULLS LAST":
q = q.OrderExpr("? ?", types.F(field), types.Q(sort))
continue loop
}
}
q.order = append(q.order, fieldAppender{order})
}
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Order",
"(",
"orders",
"...",
"string",
")",
"*",
"Query",
"{",
"loop",
":",
"for",
"_",
",",
"order",
":=",
"range",
"orders",
"{",
"if",
"order",
"==",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n",
"ind",
":=",
"strings",
".",
"Index",
"(",
"order",
",",
"\"",
"\"",
")",
"\n",
"if",
"ind",
"!=",
"-",
"1",
"{",
"field",
":=",
"order",
"[",
":",
"ind",
"]",
"\n",
"sort",
":=",
"order",
"[",
"ind",
"+",
"1",
":",
"]",
"\n",
"switch",
"internal",
".",
"UpperString",
"(",
"sort",
")",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
":",
"q",
"=",
"q",
".",
"OrderExpr",
"(",
"\"",
"\"",
",",
"types",
".",
"F",
"(",
"field",
")",
",",
"types",
".",
"Q",
"(",
"sort",
")",
")",
"\n",
"continue",
"loop",
"\n",
"}",
"\n",
"}",
"\n\n",
"q",
".",
"order",
"=",
"append",
"(",
"q",
".",
"order",
",",
"fieldAppender",
"{",
"order",
"}",
")",
"\n",
"}",
"\n",
"return",
"q",
"\n",
"}"
] |
// Order adds sort order to the Query quoting column name. Does not expand params like ?TableAlias etc.
// OrderExpr can be used to bypass quoting restriction or for params expansion.
|
[
"Order",
"adds",
"sort",
"order",
"to",
"the",
"Query",
"quoting",
"column",
"name",
".",
"Does",
"not",
"expand",
"params",
"like",
"?TableAlias",
"etc",
".",
"OrderExpr",
"can",
"be",
"used",
"to",
"bypass",
"quoting",
"restriction",
"or",
"for",
"params",
"expansion",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L551-L572
|
train
|
go-pg/pg
|
orm/query.go
|
OrderExpr
|
func (q *Query) OrderExpr(order string, params ...interface{}) *Query {
if order != "" {
q.order = append(q.order, &queryParamsAppender{order, params})
}
return q
}
|
go
|
func (q *Query) OrderExpr(order string, params ...interface{}) *Query {
if order != "" {
q.order = append(q.order, &queryParamsAppender{order, params})
}
return q
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"OrderExpr",
"(",
"order",
"string",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"*",
"Query",
"{",
"if",
"order",
"!=",
"\"",
"\"",
"{",
"q",
".",
"order",
"=",
"append",
"(",
"q",
".",
"order",
",",
"&",
"queryParamsAppender",
"{",
"order",
",",
"params",
"}",
")",
"\n",
"}",
"\n",
"return",
"q",
"\n",
"}"
] |
// Order adds sort order to the Query.
|
[
"Order",
"adds",
"sort",
"order",
"to",
"the",
"Query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L575-L580
|
train
|
go-pg/pg
|
orm/query.go
|
Apply
|
func (q *Query) Apply(fn func(*Query) (*Query, error)) *Query {
qq, err := fn(q)
if err != nil {
q.err(err)
return q
}
return qq
}
|
go
|
func (q *Query) Apply(fn func(*Query) (*Query, error)) *Query {
qq, err := fn(q)
if err != nil {
q.err(err)
return q
}
return qq
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Apply",
"(",
"fn",
"func",
"(",
"*",
"Query",
")",
"(",
"*",
"Query",
",",
"error",
")",
")",
"*",
"Query",
"{",
"qq",
",",
"err",
":=",
"fn",
"(",
"q",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"q",
".",
"err",
"(",
"err",
")",
"\n",
"return",
"q",
"\n",
"}",
"\n",
"return",
"qq",
"\n",
"}"
] |
// Apply calls the fn passing the Query as an argument.
|
[
"Apply",
"calls",
"the",
"fn",
"passing",
"the",
"Query",
"as",
"an",
"argument",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L613-L620
|
train
|
go-pg/pg
|
orm/query.go
|
Count
|
func (q *Query) Count() (int, error) {
if q.stickyErr != nil {
return 0, q.stickyErr
}
var count int
_, err := q.db.QueryOneContext(
q.ctx, Scan(&count), q.countSelectQuery("count(*)"), q.model)
return count, err
}
|
go
|
func (q *Query) Count() (int, error) {
if q.stickyErr != nil {
return 0, q.stickyErr
}
var count int
_, err := q.db.QueryOneContext(
q.ctx, Scan(&count), q.countSelectQuery("count(*)"), q.model)
return count, err
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Count",
"(",
")",
"(",
"int",
",",
"error",
")",
"{",
"if",
"q",
".",
"stickyErr",
"!=",
"nil",
"{",
"return",
"0",
",",
"q",
".",
"stickyErr",
"\n",
"}",
"\n\n",
"var",
"count",
"int",
"\n",
"_",
",",
"err",
":=",
"q",
".",
"db",
".",
"QueryOneContext",
"(",
"q",
".",
"ctx",
",",
"Scan",
"(",
"&",
"count",
")",
",",
"q",
".",
"countSelectQuery",
"(",
"\"",
"\"",
")",
",",
"q",
".",
"model",
")",
"\n",
"return",
"count",
",",
"err",
"\n",
"}"
] |
// Count returns number of rows matching the query using count aggregate function.
|
[
"Count",
"returns",
"number",
"of",
"rows",
"matching",
"the",
"query",
"using",
"count",
"aggregate",
"function",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L623-L632
|
train
|
go-pg/pg
|
orm/query.go
|
Select
|
func (q *Query) Select(values ...interface{}) error {
if q.stickyErr != nil {
return q.stickyErr
}
model, err := q.newModel(values...)
if err != nil {
return err
}
q, err = model.BeforeSelectQuery(q.ctx, q.db, q)
if err != nil {
return err
}
res, err := q.query(model, newSelectQuery(q))
if err != nil {
return err
}
if res.RowsReturned() > 0 {
if q.model != nil {
if err := q.selectJoins(q.model.GetJoins()); err != nil {
return err
}
}
if err := model.AfterSelect(q.ctx, q.db); err != nil {
return err
}
}
return nil
}
|
go
|
func (q *Query) Select(values ...interface{}) error {
if q.stickyErr != nil {
return q.stickyErr
}
model, err := q.newModel(values...)
if err != nil {
return err
}
q, err = model.BeforeSelectQuery(q.ctx, q.db, q)
if err != nil {
return err
}
res, err := q.query(model, newSelectQuery(q))
if err != nil {
return err
}
if res.RowsReturned() > 0 {
if q.model != nil {
if err := q.selectJoins(q.model.GetJoins()); err != nil {
return err
}
}
if err := model.AfterSelect(q.ctx, q.db); err != nil {
return err
}
}
return nil
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Select",
"(",
"values",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"q",
".",
"stickyErr",
"!=",
"nil",
"{",
"return",
"q",
".",
"stickyErr",
"\n",
"}",
"\n\n",
"model",
",",
"err",
":=",
"q",
".",
"newModel",
"(",
"values",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"q",
",",
"err",
"=",
"model",
".",
"BeforeSelectQuery",
"(",
"q",
".",
"ctx",
",",
"q",
".",
"db",
",",
"q",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"q",
".",
"query",
"(",
"model",
",",
"newSelectQuery",
"(",
"q",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"res",
".",
"RowsReturned",
"(",
")",
">",
"0",
"{",
"if",
"q",
".",
"model",
"!=",
"nil",
"{",
"if",
"err",
":=",
"q",
".",
"selectJoins",
"(",
"q",
".",
"model",
".",
"GetJoins",
"(",
")",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"err",
":=",
"model",
".",
"AfterSelect",
"(",
"q",
".",
"ctx",
",",
"q",
".",
"db",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Select selects the model.
|
[
"Select",
"selects",
"the",
"model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L672-L704
|
train
|
go-pg/pg
|
orm/query.go
|
SelectAndCount
|
func (q *Query) SelectAndCount(values ...interface{}) (count int, firstErr error) {
if q.stickyErr != nil {
return 0, q.stickyErr
}
var wg sync.WaitGroup
var mu sync.Mutex
if q.limit >= 0 {
wg.Add(1)
go func() {
defer wg.Done()
err := q.Select(values...)
if err != nil {
mu.Lock()
if firstErr == nil {
firstErr = err
}
mu.Unlock()
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
var err error
count, err = q.Count()
if err != nil {
mu.Lock()
if firstErr == nil {
firstErr = err
}
mu.Unlock()
}
}()
wg.Wait()
return count, firstErr
}
|
go
|
func (q *Query) SelectAndCount(values ...interface{}) (count int, firstErr error) {
if q.stickyErr != nil {
return 0, q.stickyErr
}
var wg sync.WaitGroup
var mu sync.Mutex
if q.limit >= 0 {
wg.Add(1)
go func() {
defer wg.Done()
err := q.Select(values...)
if err != nil {
mu.Lock()
if firstErr == nil {
firstErr = err
}
mu.Unlock()
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
var err error
count, err = q.Count()
if err != nil {
mu.Lock()
if firstErr == nil {
firstErr = err
}
mu.Unlock()
}
}()
wg.Wait()
return count, firstErr
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"SelectAndCount",
"(",
"values",
"...",
"interface",
"{",
"}",
")",
"(",
"count",
"int",
",",
"firstErr",
"error",
")",
"{",
"if",
"q",
".",
"stickyErr",
"!=",
"nil",
"{",
"return",
"0",
",",
"q",
".",
"stickyErr",
"\n",
"}",
"\n\n",
"var",
"wg",
"sync",
".",
"WaitGroup",
"\n",
"var",
"mu",
"sync",
".",
"Mutex",
"\n\n",
"if",
"q",
".",
"limit",
">=",
"0",
"{",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"defer",
"wg",
".",
"Done",
"(",
")",
"\n",
"err",
":=",
"q",
".",
"Select",
"(",
"values",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"mu",
".",
"Lock",
"(",
")",
"\n",
"if",
"firstErr",
"==",
"nil",
"{",
"firstErr",
"=",
"err",
"\n",
"}",
"\n",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"}",
"\n\n",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"defer",
"wg",
".",
"Done",
"(",
")",
"\n",
"var",
"err",
"error",
"\n",
"count",
",",
"err",
"=",
"q",
".",
"Count",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"mu",
".",
"Lock",
"(",
")",
"\n",
"if",
"firstErr",
"==",
"nil",
"{",
"firstErr",
"=",
"err",
"\n",
"}",
"\n",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n\n",
"wg",
".",
"Wait",
"(",
")",
"\n",
"return",
"count",
",",
"firstErr",
"\n",
"}"
] |
// SelectAndCount runs Select and Count in two goroutines,
// waits for them to finish and returns the result. If query limit is -1
// it does not select any data and only counts the results.
|
[
"SelectAndCount",
"runs",
"Select",
"and",
"Count",
"in",
"two",
"goroutines",
"waits",
"for",
"them",
"to",
"finish",
"and",
"returns",
"the",
"result",
".",
"If",
"query",
"limit",
"is",
"-",
"1",
"it",
"does",
"not",
"select",
"any",
"data",
"and",
"only",
"counts",
"the",
"results",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L723-L762
|
train
|
go-pg/pg
|
orm/query.go
|
ForEach
|
func (q *Query) ForEach(fn interface{}) error {
m := newFuncModel(fn)
return q.Select(m)
}
|
go
|
func (q *Query) ForEach(fn interface{}) error {
m := newFuncModel(fn)
return q.Select(m)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"ForEach",
"(",
"fn",
"interface",
"{",
"}",
")",
"error",
"{",
"m",
":=",
"newFuncModel",
"(",
"fn",
")",
"\n",
"return",
"q",
".",
"Select",
"(",
"m",
")",
"\n",
"}"
] |
// ForEach calls the function for each row returned by the query
// without loading all rows into the memory.
// Function accepts a struct, pointer to a struct, orm.Model,
// or values for columns in a row. Function must return an error.
|
[
"ForEach",
"calls",
"the",
"function",
"for",
"each",
"row",
"returned",
"by",
"the",
"query",
"without",
"loading",
"all",
"rows",
"into",
"the",
"memory",
".",
"Function",
"accepts",
"a",
"struct",
"pointer",
"to",
"a",
"struct",
"orm",
".",
"Model",
"or",
"values",
"for",
"columns",
"in",
"a",
"row",
".",
"Function",
"must",
"return",
"an",
"error",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L812-L815
|
train
|
go-pg/pg
|
orm/query.go
|
Insert
|
func (q *Query) Insert(values ...interface{}) (Result, error) {
if q.stickyErr != nil {
return nil, q.stickyErr
}
model, err := q.newModel(values...)
if err != nil {
return nil, err
}
if q.model != nil && q.model.Table().HasFlag(BeforeInsertHookFlag) {
err = q.model.BeforeInsert(q.ctx, q.db)
if err != nil {
return nil, err
}
}
query := newInsertQuery(q)
res, err := q.returningQuery(model, query)
if err != nil {
return nil, err
}
if q.model != nil {
err = q.model.AfterInsert(q.ctx, q.db)
if err != nil {
return nil, err
}
}
return res, nil
}
|
go
|
func (q *Query) Insert(values ...interface{}) (Result, error) {
if q.stickyErr != nil {
return nil, q.stickyErr
}
model, err := q.newModel(values...)
if err != nil {
return nil, err
}
if q.model != nil && q.model.Table().HasFlag(BeforeInsertHookFlag) {
err = q.model.BeforeInsert(q.ctx, q.db)
if err != nil {
return nil, err
}
}
query := newInsertQuery(q)
res, err := q.returningQuery(model, query)
if err != nil {
return nil, err
}
if q.model != nil {
err = q.model.AfterInsert(q.ctx, q.db)
if err != nil {
return nil, err
}
}
return res, nil
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Insert",
"(",
"values",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"if",
"q",
".",
"stickyErr",
"!=",
"nil",
"{",
"return",
"nil",
",",
"q",
".",
"stickyErr",
"\n",
"}",
"\n\n",
"model",
",",
"err",
":=",
"q",
".",
"newModel",
"(",
"values",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"q",
".",
"model",
"!=",
"nil",
"&&",
"q",
".",
"model",
".",
"Table",
"(",
")",
".",
"HasFlag",
"(",
"BeforeInsertHookFlag",
")",
"{",
"err",
"=",
"q",
".",
"model",
".",
"BeforeInsert",
"(",
"q",
".",
"ctx",
",",
"q",
".",
"db",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"query",
":=",
"newInsertQuery",
"(",
"q",
")",
"\n",
"res",
",",
"err",
":=",
"q",
".",
"returningQuery",
"(",
"model",
",",
"query",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"q",
".",
"model",
"!=",
"nil",
"{",
"err",
"=",
"q",
".",
"model",
".",
"AfterInsert",
"(",
"q",
".",
"ctx",
",",
"q",
".",
"db",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] |
// Insert inserts the model.
|
[
"Insert",
"inserts",
"the",
"model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L860-L891
|
train
|
go-pg/pg
|
orm/query.go
|
SelectOrInsert
|
func (q *Query) SelectOrInsert(values ...interface{}) (inserted bool, _ error) {
if q.stickyErr != nil {
return false, q.stickyErr
}
var insertq *Query
var insertErr error
for i := 0; i < 5; i++ {
if i >= 2 {
time.Sleep(internal.RetryBackoff(i-2, 250*time.Millisecond, 5*time.Second))
}
err := q.Select(values...)
if err == nil {
return false, nil
}
if err != internal.ErrNoRows {
return false, err
}
if insertq == nil {
insertq = q
if len(insertq.columns) > 0 {
insertq = insertq.Copy()
insertq.columns = nil
}
}
res, err := insertq.Insert(values...)
if err != nil {
insertErr = err
if err == internal.ErrNoRows {
continue
}
if pgErr, ok := err.(internal.PGError); ok {
if pgErr.IntegrityViolation() {
continue
}
if pgErr.Field('C') == "55000" {
// Retry on "#55000 attempted to delete invisible tuple".
continue
}
}
return false, err
}
if res.RowsAffected() == 1 {
return true, nil
}
}
err := fmt.Errorf(
"pg: SelectOrInsert: select returns no rows (insert fails with err=%q)",
insertErr)
return false, err
}
|
go
|
func (q *Query) SelectOrInsert(values ...interface{}) (inserted bool, _ error) {
if q.stickyErr != nil {
return false, q.stickyErr
}
var insertq *Query
var insertErr error
for i := 0; i < 5; i++ {
if i >= 2 {
time.Sleep(internal.RetryBackoff(i-2, 250*time.Millisecond, 5*time.Second))
}
err := q.Select(values...)
if err == nil {
return false, nil
}
if err != internal.ErrNoRows {
return false, err
}
if insertq == nil {
insertq = q
if len(insertq.columns) > 0 {
insertq = insertq.Copy()
insertq.columns = nil
}
}
res, err := insertq.Insert(values...)
if err != nil {
insertErr = err
if err == internal.ErrNoRows {
continue
}
if pgErr, ok := err.(internal.PGError); ok {
if pgErr.IntegrityViolation() {
continue
}
if pgErr.Field('C') == "55000" {
// Retry on "#55000 attempted to delete invisible tuple".
continue
}
}
return false, err
}
if res.RowsAffected() == 1 {
return true, nil
}
}
err := fmt.Errorf(
"pg: SelectOrInsert: select returns no rows (insert fails with err=%q)",
insertErr)
return false, err
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"SelectOrInsert",
"(",
"values",
"...",
"interface",
"{",
"}",
")",
"(",
"inserted",
"bool",
",",
"_",
"error",
")",
"{",
"if",
"q",
".",
"stickyErr",
"!=",
"nil",
"{",
"return",
"false",
",",
"q",
".",
"stickyErr",
"\n",
"}",
"\n\n",
"var",
"insertq",
"*",
"Query",
"\n",
"var",
"insertErr",
"error",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"5",
";",
"i",
"++",
"{",
"if",
"i",
">=",
"2",
"{",
"time",
".",
"Sleep",
"(",
"internal",
".",
"RetryBackoff",
"(",
"i",
"-",
"2",
",",
"250",
"*",
"time",
".",
"Millisecond",
",",
"5",
"*",
"time",
".",
"Second",
")",
")",
"\n",
"}",
"\n\n",
"err",
":=",
"q",
".",
"Select",
"(",
"values",
"...",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"internal",
".",
"ErrNoRows",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"insertq",
"==",
"nil",
"{",
"insertq",
"=",
"q",
"\n",
"if",
"len",
"(",
"insertq",
".",
"columns",
")",
">",
"0",
"{",
"insertq",
"=",
"insertq",
".",
"Copy",
"(",
")",
"\n",
"insertq",
".",
"columns",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"insertq",
".",
"Insert",
"(",
"values",
"...",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"insertErr",
"=",
"err",
"\n",
"if",
"err",
"==",
"internal",
".",
"ErrNoRows",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"pgErr",
",",
"ok",
":=",
"err",
".",
"(",
"internal",
".",
"PGError",
")",
";",
"ok",
"{",
"if",
"pgErr",
".",
"IntegrityViolation",
"(",
")",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"pgErr",
".",
"Field",
"(",
"'C'",
")",
"==",
"\"",
"\"",
"{",
"// Retry on \"#55000 attempted to delete invisible tuple\".",
"continue",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
",",
"err",
"\n",
"}",
"\n",
"if",
"res",
".",
"RowsAffected",
"(",
")",
"==",
"1",
"{",
"return",
"true",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"err",
":=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"insertErr",
")",
"\n",
"return",
"false",
",",
"err",
"\n",
"}"
] |
// SelectOrInsert selects the model inserting one if it does not exist.
// It returns true when model was inserted.
|
[
"SelectOrInsert",
"selects",
"the",
"model",
"inserting",
"one",
"if",
"it",
"does",
"not",
"exist",
".",
"It",
"returns",
"true",
"when",
"model",
"was",
"inserted",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L895-L949
|
train
|
go-pg/pg
|
orm/query.go
|
Update
|
func (q *Query) Update(scan ...interface{}) (Result, error) {
return q.update(scan, false)
}
|
go
|
func (q *Query) Update(scan ...interface{}) (Result, error) {
return q.update(scan, false)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Update",
"(",
"scan",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"q",
".",
"update",
"(",
"scan",
",",
"false",
")",
"\n",
"}"
] |
// Update updates the model.
|
[
"Update",
"updates",
"the",
"model",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L952-L954
|
train
|
go-pg/pg
|
orm/query.go
|
UpdateNotNull
|
func (q *Query) UpdateNotNull(scan ...interface{}) (Result, error) {
return q.update(scan, true)
}
|
go
|
func (q *Query) UpdateNotNull(scan ...interface{}) (Result, error) {
return q.update(scan, true)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"UpdateNotNull",
"(",
"scan",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"return",
"q",
".",
"update",
"(",
"scan",
",",
"true",
")",
"\n",
"}"
] |
// Update updates the model omitting null columns.
|
[
"Update",
"updates",
"the",
"model",
"omitting",
"null",
"columns",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L957-L959
|
train
|
go-pg/pg
|
orm/query.go
|
Delete
|
func (q *Query) Delete(values ...interface{}) (Result, error) {
if q.hasModel() {
table := q.model.Table()
if table.SoftDeleteField != nil {
q.model.setSoftDeleteField()
columns := q.columns
q.columns = nil
res, err := q.Column(table.SoftDeleteField.SQLName).Update(values...)
q.columns = columns
return res, err
}
}
return q.ForceDelete(values...)
}
|
go
|
func (q *Query) Delete(values ...interface{}) (Result, error) {
if q.hasModel() {
table := q.model.Table()
if table.SoftDeleteField != nil {
q.model.setSoftDeleteField()
columns := q.columns
q.columns = nil
res, err := q.Column(table.SoftDeleteField.SQLName).Update(values...)
q.columns = columns
return res, err
}
}
return q.ForceDelete(values...)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Delete",
"(",
"values",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"if",
"q",
".",
"hasModel",
"(",
")",
"{",
"table",
":=",
"q",
".",
"model",
".",
"Table",
"(",
")",
"\n",
"if",
"table",
".",
"SoftDeleteField",
"!=",
"nil",
"{",
"q",
".",
"model",
".",
"setSoftDeleteField",
"(",
")",
"\n",
"columns",
":=",
"q",
".",
"columns",
"\n",
"q",
".",
"columns",
"=",
"nil",
"\n",
"res",
",",
"err",
":=",
"q",
".",
"Column",
"(",
"table",
".",
"SoftDeleteField",
".",
"SQLName",
")",
".",
"Update",
"(",
"values",
"...",
")",
"\n",
"q",
".",
"columns",
"=",
"columns",
"\n",
"return",
"res",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"q",
".",
"ForceDelete",
"(",
"values",
"...",
")",
"\n",
"}"
] |
// Delete deletes the model. When model has deleted_at column the row
// is soft deleted instead.
|
[
"Delete",
"deletes",
"the",
"model",
".",
"When",
"model",
"has",
"deleted_at",
"column",
"the",
"row",
"is",
"soft",
"deleted",
"instead",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L1006-L1019
|
train
|
go-pg/pg
|
orm/query.go
|
CopyFrom
|
func (q *Query) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error) {
params = append(params, q.model)
return q.db.CopyFrom(r, query, params...)
}
|
go
|
func (q *Query) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error) {
params = append(params, q.model)
return q.db.CopyFrom(r, query, params...)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"CopyFrom",
"(",
"r",
"io",
".",
"Reader",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"q",
".",
"model",
")",
"\n",
"return",
"q",
".",
"db",
".",
"CopyFrom",
"(",
"r",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// CopyFrom is an alias from DB.CopyFrom.
|
[
"CopyFrom",
"is",
"an",
"alias",
"from",
"DB",
".",
"CopyFrom",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L1093-L1096
|
train
|
go-pg/pg
|
orm/query.go
|
CopyTo
|
func (q *Query) CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error) {
params = append(params, q.model)
return q.db.CopyTo(w, query, params...)
}
|
go
|
func (q *Query) CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error) {
params = append(params, q.model)
return q.db.CopyTo(w, query, params...)
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"CopyTo",
"(",
"w",
"io",
".",
"Writer",
",",
"query",
"interface",
"{",
"}",
",",
"params",
"...",
"interface",
"{",
"}",
")",
"(",
"Result",
",",
"error",
")",
"{",
"params",
"=",
"append",
"(",
"params",
",",
"q",
".",
"model",
")",
"\n",
"return",
"q",
".",
"db",
".",
"CopyTo",
"(",
"w",
",",
"query",
",",
"params",
"...",
")",
"\n",
"}"
] |
// CopyTo is an alias from DB.CopyTo.
|
[
"CopyTo",
"is",
"an",
"alias",
"from",
"DB",
".",
"CopyTo",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L1099-L1102
|
train
|
go-pg/pg
|
orm/query.go
|
Exists
|
func (q *Query) Exists() (bool, error) {
cp := q.Copy() // copy to not change original query
cp.columns = []QueryAppender{Q("1")}
cp.order = nil
cp.limit = 1
res, err := q.db.ExecContext(q.ctx, newSelectQuery(cp))
if err != nil {
return false, err
}
return res.RowsAffected() > 0, nil
}
|
go
|
func (q *Query) Exists() (bool, error) {
cp := q.Copy() // copy to not change original query
cp.columns = []QueryAppender{Q("1")}
cp.order = nil
cp.limit = 1
res, err := q.db.ExecContext(q.ctx, newSelectQuery(cp))
if err != nil {
return false, err
}
return res.RowsAffected() > 0, nil
}
|
[
"func",
"(",
"q",
"*",
"Query",
")",
"Exists",
"(",
")",
"(",
"bool",
",",
"error",
")",
"{",
"cp",
":=",
"q",
".",
"Copy",
"(",
")",
"// copy to not change original query",
"\n",
"cp",
".",
"columns",
"=",
"[",
"]",
"QueryAppender",
"{",
"Q",
"(",
"\"",
"\"",
")",
"}",
"\n",
"cp",
".",
"order",
"=",
"nil",
"\n",
"cp",
".",
"limit",
"=",
"1",
"\n",
"res",
",",
"err",
":=",
"q",
".",
"db",
".",
"ExecContext",
"(",
"q",
".",
"ctx",
",",
"newSelectQuery",
"(",
"cp",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
",",
"err",
"\n",
"}",
"\n",
"return",
"res",
".",
"RowsAffected",
"(",
")",
">",
"0",
",",
"nil",
"\n",
"}"
] |
// Exists returns true or false depending if there are any rows matching the query.
|
[
"Exists",
"returns",
"true",
"or",
"false",
"depending",
"if",
"there",
"are",
"any",
"rows",
"matching",
"the",
"query",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/orm/query.go#L1111-L1121
|
train
|
go-pg/pg
|
db.go
|
Connect
|
func Connect(opt *Options) *DB {
opt.init()
return newDB(
context.TODO(),
&baseDB{
opt: opt,
pool: newConnPool(opt),
},
)
}
|
go
|
func Connect(opt *Options) *DB {
opt.init()
return newDB(
context.TODO(),
&baseDB{
opt: opt,
pool: newConnPool(opt),
},
)
}
|
[
"func",
"Connect",
"(",
"opt",
"*",
"Options",
")",
"*",
"DB",
"{",
"opt",
".",
"init",
"(",
")",
"\n",
"return",
"newDB",
"(",
"context",
".",
"TODO",
"(",
")",
",",
"&",
"baseDB",
"{",
"opt",
":",
"opt",
",",
"pool",
":",
"newConnPool",
"(",
"opt",
")",
",",
"}",
",",
")",
"\n",
"}"
] |
// Connect connects to a database using provided options.
//
// The returned DB is safe for concurrent use by multiple goroutines
// and maintains its own connection pool.
|
[
"Connect",
"connects",
"to",
"a",
"database",
"using",
"provided",
"options",
".",
"The",
"returned",
"DB",
"is",
"safe",
"for",
"concurrent",
"use",
"by",
"multiple",
"goroutines",
"and",
"maintains",
"its",
"own",
"connection",
"pool",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/db.go#L16-L25
|
train
|
go-pg/pg
|
db.go
|
Listen
|
func (db *DB) Listen(channels ...string) *Listener {
ln := &Listener{
db: db,
}
ln.init()
_ = ln.Listen(channels...)
return ln
}
|
go
|
func (db *DB) Listen(channels ...string) *Listener {
ln := &Listener{
db: db,
}
ln.init()
_ = ln.Listen(channels...)
return ln
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Listen",
"(",
"channels",
"...",
"string",
")",
"*",
"Listener",
"{",
"ln",
":=",
"&",
"Listener",
"{",
"db",
":",
"db",
",",
"}",
"\n",
"ln",
".",
"init",
"(",
")",
"\n",
"_",
"=",
"ln",
".",
"Listen",
"(",
"channels",
"...",
")",
"\n",
"return",
"ln",
"\n",
"}"
] |
// Listen listens for notifications sent with NOTIFY command.
|
[
"Listen",
"listens",
"for",
"notifications",
"sent",
"with",
"NOTIFY",
"command",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/db.go#L80-L87
|
train
|
go-pg/pg
|
db.go
|
Conn
|
func (db *DB) Conn() *Conn {
return newConn(db.ctx, db.baseDB.withPool(pool.NewSingleConnPool(db.pool)))
}
|
go
|
func (db *DB) Conn() *Conn {
return newConn(db.ctx, db.baseDB.withPool(pool.NewSingleConnPool(db.pool)))
}
|
[
"func",
"(",
"db",
"*",
"DB",
")",
"Conn",
"(",
")",
"*",
"Conn",
"{",
"return",
"newConn",
"(",
"db",
".",
"ctx",
",",
"db",
".",
"baseDB",
".",
"withPool",
"(",
"pool",
".",
"NewSingleConnPool",
"(",
"db",
".",
"pool",
")",
")",
")",
"\n",
"}"
] |
// Conn returns a single connection by either opening a new connection
// or returning an existing connection from the connection pool. Conn will
// block until either a connection is returned or ctx is canceled.
// Queries run on the same Conn will be run in the same database session.
//
// Every Conn must be returned to the database pool after use by
// calling Conn.Close.
|
[
"Conn",
"returns",
"a",
"single",
"connection",
"by",
"either",
"opening",
"a",
"new",
"connection",
"or",
"returning",
"an",
"existing",
"connection",
"from",
"the",
"connection",
"pool",
".",
"Conn",
"will",
"block",
"until",
"either",
"a",
"connection",
"is",
"returned",
"or",
"ctx",
"is",
"canceled",
".",
"Queries",
"run",
"on",
"the",
"same",
"Conn",
"will",
"be",
"run",
"in",
"the",
"same",
"database",
"session",
".",
"Every",
"Conn",
"must",
"be",
"returned",
"to",
"the",
"database",
"pool",
"after",
"use",
"by",
"calling",
"Conn",
".",
"Close",
"."
] |
9ab5ba23b6047052b91007b7041d9216025219a8
|
https://github.com/go-pg/pg/blob/9ab5ba23b6047052b91007b7041d9216025219a8/db.go#L111-L113
|
train
|
sendgrid/sendgrid-go
|
examples/helpers/mail/example.go
|
helloEmail
|
func helloEmail() []byte {
address := "test@example.com"
name := "Example User"
from := mail.NewEmail(name, address)
subject := "Hello World from the SendGrid Go Library"
address = "test@example.com"
name = "Example User"
to := mail.NewEmail(name, address)
content := mail.NewContent("text/plain", "some text here")
m := mail.NewV3MailInit(from, subject, to, content)
address = "test2@example.com"
name = "Example User"
email := mail.NewEmail(name, address)
m.Personalizations[0].AddTos(email)
return mail.GetRequestBody(m)
}
|
go
|
func helloEmail() []byte {
address := "test@example.com"
name := "Example User"
from := mail.NewEmail(name, address)
subject := "Hello World from the SendGrid Go Library"
address = "test@example.com"
name = "Example User"
to := mail.NewEmail(name, address)
content := mail.NewContent("text/plain", "some text here")
m := mail.NewV3MailInit(from, subject, to, content)
address = "test2@example.com"
name = "Example User"
email := mail.NewEmail(name, address)
m.Personalizations[0].AddTos(email)
return mail.GetRequestBody(m)
}
|
[
"func",
"helloEmail",
"(",
")",
"[",
"]",
"byte",
"{",
"address",
":=",
"\"",
"\"",
"\n",
"name",
":=",
"\"",
"\"",
"\n",
"from",
":=",
"mail",
".",
"NewEmail",
"(",
"name",
",",
"address",
")",
"\n",
"subject",
":=",
"\"",
"\"",
"\n",
"address",
"=",
"\"",
"\"",
"\n",
"name",
"=",
"\"",
"\"",
"\n",
"to",
":=",
"mail",
".",
"NewEmail",
"(",
"name",
",",
"address",
")",
"\n",
"content",
":=",
"mail",
".",
"NewContent",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"m",
":=",
"mail",
".",
"NewV3MailInit",
"(",
"from",
",",
"subject",
",",
"to",
",",
"content",
")",
"\n",
"address",
"=",
"\"",
"\"",
"\n",
"name",
"=",
"\"",
"\"",
"\n",
"email",
":=",
"mail",
".",
"NewEmail",
"(",
"name",
",",
"address",
")",
"\n",
"m",
".",
"Personalizations",
"[",
"0",
"]",
".",
"AddTos",
"(",
"email",
")",
"\n",
"return",
"mail",
".",
"GetRequestBody",
"(",
"m",
")",
"\n",
"}"
] |
// Minimum required to send an email
|
[
"Minimum",
"required",
"to",
"send",
"an",
"email"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/examples/helpers/mail/example.go#L14-L29
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
GetRequestSubuser
|
func GetRequestSubuser(key, endpoint, host, subuser string) rest.Request {
return requestNew(options{key, endpoint, host, subuser})
}
|
go
|
func GetRequestSubuser(key, endpoint, host, subuser string) rest.Request {
return requestNew(options{key, endpoint, host, subuser})
}
|
[
"func",
"GetRequestSubuser",
"(",
"key",
",",
"endpoint",
",",
"host",
",",
"subuser",
"string",
")",
"rest",
".",
"Request",
"{",
"return",
"requestNew",
"(",
"options",
"{",
"key",
",",
"endpoint",
",",
"host",
",",
"subuser",
"}",
")",
"\n",
"}"
] |
// GetRequestSubuser like GetRequest but with On-Behalf of Subuser
// @return [Request] a default request object
|
[
"GetRequestSubuser",
"like",
"GetRequest",
"but",
"with",
"On",
"-",
"Behalf",
"of",
"Subuser"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L47-L49
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
requestNew
|
func requestNew(options options) rest.Request {
if options.Host == "" {
options.Host = "https://api.sendgrid.com"
}
requestHeaders := map[string]string{
"Authorization": "Bearer " + options.Key,
"User-Agent": "sendgrid/" + Version + ";go",
"Accept": "application/json",
}
if len(options.Subuser) != 0 {
requestHeaders["On-Behalf-Of"] = options.Subuser
}
return rest.Request{
BaseURL: options.baseURL(),
Headers: requestHeaders,
}
}
|
go
|
func requestNew(options options) rest.Request {
if options.Host == "" {
options.Host = "https://api.sendgrid.com"
}
requestHeaders := map[string]string{
"Authorization": "Bearer " + options.Key,
"User-Agent": "sendgrid/" + Version + ";go",
"Accept": "application/json",
}
if len(options.Subuser) != 0 {
requestHeaders["On-Behalf-Of"] = options.Subuser
}
return rest.Request{
BaseURL: options.baseURL(),
Headers: requestHeaders,
}
}
|
[
"func",
"requestNew",
"(",
"options",
"options",
")",
"rest",
".",
"Request",
"{",
"if",
"options",
".",
"Host",
"==",
"\"",
"\"",
"{",
"options",
".",
"Host",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"requestHeaders",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"\"",
"\"",
"+",
"options",
".",
"Key",
",",
"\"",
"\"",
":",
"\"",
"\"",
"+",
"Version",
"+",
"\"",
"\"",
",",
"\"",
"\"",
":",
"\"",
"\"",
",",
"}",
"\n\n",
"if",
"len",
"(",
"options",
".",
"Subuser",
")",
"!=",
"0",
"{",
"requestHeaders",
"[",
"\"",
"\"",
"]",
"=",
"options",
".",
"Subuser",
"\n",
"}",
"\n\n",
"return",
"rest",
".",
"Request",
"{",
"BaseURL",
":",
"options",
".",
"baseURL",
"(",
")",
",",
"Headers",
":",
"requestHeaders",
",",
"}",
"\n",
"}"
] |
// requestNew create Request
// @return [Request] a default request object
|
[
"requestNew",
"create",
"Request"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L53-L72
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
Send
|
func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
cl.Body = mail.GetRequestBody(email)
return MakeRequest(cl.Request)
}
|
go
|
func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
cl.Body = mail.GetRequestBody(email)
return MakeRequest(cl.Request)
}
|
[
"func",
"(",
"cl",
"*",
"Client",
")",
"Send",
"(",
"email",
"*",
"mail",
".",
"SGMailV3",
")",
"(",
"*",
"rest",
".",
"Response",
",",
"error",
")",
"{",
"cl",
".",
"Body",
"=",
"mail",
".",
"GetRequestBody",
"(",
"email",
")",
"\n",
"return",
"MakeRequest",
"(",
"cl",
".",
"Request",
")",
"\n",
"}"
] |
// Send sends an email through SendGrid
|
[
"Send",
"sends",
"an",
"email",
"through",
"SendGrid"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L75-L78
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
NewSendClient
|
func NewSendClient(key string) *Client {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &Client{request}
}
|
go
|
func NewSendClient(key string) *Client {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &Client{request}
}
|
[
"func",
"NewSendClient",
"(",
"key",
"string",
")",
"*",
"Client",
"{",
"request",
":=",
"GetRequest",
"(",
"key",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"request",
".",
"Method",
"=",
"\"",
"\"",
"\n",
"return",
"&",
"Client",
"{",
"request",
"}",
"\n",
"}"
] |
// NewSendClient constructs a new SendGrid client given an API key
|
[
"NewSendClient",
"constructs",
"a",
"new",
"SendGrid",
"client",
"given",
"an",
"API",
"key"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L81-L85
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
NewSendClientSubuser
|
func NewSendClientSubuser(key, subuser string) *Client {
request := GetRequestSubuser(key, "/v3/mail/send", "", subuser)
request.Method = "POST"
return &Client{request}
}
|
go
|
func NewSendClientSubuser(key, subuser string) *Client {
request := GetRequestSubuser(key, "/v3/mail/send", "", subuser)
request.Method = "POST"
return &Client{request}
}
|
[
"func",
"NewSendClientSubuser",
"(",
"key",
",",
"subuser",
"string",
")",
"*",
"Client",
"{",
"request",
":=",
"GetRequestSubuser",
"(",
"key",
",",
"\"",
"\"",
",",
"\"",
"\"",
",",
"subuser",
")",
"\n",
"request",
".",
"Method",
"=",
"\"",
"\"",
"\n",
"return",
"&",
"Client",
"{",
"request",
"}",
"\n",
"}"
] |
// GetRequestSubuser like NewSendClient but with On-Behalf of Subuser
// @return [Client]
|
[
"GetRequestSubuser",
"like",
"NewSendClient",
"but",
"with",
"On",
"-",
"Behalf",
"of",
"Subuser"
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L89-L93
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
MakeRequest
|
func MakeRequest(request rest.Request) (*rest.Response, error) {
return DefaultClient.Send(request)
}
|
go
|
func MakeRequest(request rest.Request) (*rest.Response, error) {
return DefaultClient.Send(request)
}
|
[
"func",
"MakeRequest",
"(",
"request",
"rest",
".",
"Request",
")",
"(",
"*",
"rest",
".",
"Response",
",",
"error",
")",
"{",
"return",
"DefaultClient",
".",
"Send",
"(",
"request",
")",
"\n",
"}"
] |
// MakeRequest attempts a SendGrid request synchronously.
|
[
"MakeRequest",
"attempts",
"a",
"SendGrid",
"request",
"synchronously",
"."
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L106-L108
|
train
|
sendgrid/sendgrid-go
|
sendgrid.go
|
MakeRequestRetry
|
func MakeRequestRetry(request rest.Request) (*rest.Response, error) {
retry := 0
var response *rest.Response
var err error
for {
response, err = MakeRequest(request)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusTooManyRequests {
return response, nil
}
if retry > rateLimitRetry {
return nil, errors.New("Rate limit retry exceeded")
}
retry++
resetTime := time.Now().Add(rateLimitSleep * time.Millisecond)
reset, ok := response.Headers["X-RateLimit-Reset"]
if ok && len(reset) > 0 {
t, err := strconv.Atoi(reset[0])
if err == nil {
resetTime = time.Unix(int64(t), 0)
}
}
time.Sleep(resetTime.Sub(time.Now()))
}
}
|
go
|
func MakeRequestRetry(request rest.Request) (*rest.Response, error) {
retry := 0
var response *rest.Response
var err error
for {
response, err = MakeRequest(request)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusTooManyRequests {
return response, nil
}
if retry > rateLimitRetry {
return nil, errors.New("Rate limit retry exceeded")
}
retry++
resetTime := time.Now().Add(rateLimitSleep * time.Millisecond)
reset, ok := response.Headers["X-RateLimit-Reset"]
if ok && len(reset) > 0 {
t, err := strconv.Atoi(reset[0])
if err == nil {
resetTime = time.Unix(int64(t), 0)
}
}
time.Sleep(resetTime.Sub(time.Now()))
}
}
|
[
"func",
"MakeRequestRetry",
"(",
"request",
"rest",
".",
"Request",
")",
"(",
"*",
"rest",
".",
"Response",
",",
"error",
")",
"{",
"retry",
":=",
"0",
"\n",
"var",
"response",
"*",
"rest",
".",
"Response",
"\n",
"var",
"err",
"error",
"\n\n",
"for",
"{",
"response",
",",
"err",
"=",
"MakeRequest",
"(",
"request",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"response",
".",
"StatusCode",
"!=",
"http",
".",
"StatusTooManyRequests",
"{",
"return",
"response",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"retry",
">",
"rateLimitRetry",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"retry",
"++",
"\n\n",
"resetTime",
":=",
"time",
".",
"Now",
"(",
")",
".",
"Add",
"(",
"rateLimitSleep",
"*",
"time",
".",
"Millisecond",
")",
"\n\n",
"reset",
",",
"ok",
":=",
"response",
".",
"Headers",
"[",
"\"",
"\"",
"]",
"\n",
"if",
"ok",
"&&",
"len",
"(",
"reset",
")",
">",
"0",
"{",
"t",
",",
"err",
":=",
"strconv",
".",
"Atoi",
"(",
"reset",
"[",
"0",
"]",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"resetTime",
"=",
"time",
".",
"Unix",
"(",
"int64",
"(",
"t",
")",
",",
"0",
")",
"\n",
"}",
"\n",
"}",
"\n",
"time",
".",
"Sleep",
"(",
"resetTime",
".",
"Sub",
"(",
"time",
".",
"Now",
"(",
")",
")",
")",
"\n",
"}",
"\n",
"}"
] |
// MakeRequestRetry a synchronous request, but retry in the event of a rate
// limited response.
|
[
"MakeRequestRetry",
"a",
"synchronous",
"request",
"but",
"retry",
"in",
"the",
"event",
"of",
"a",
"rate",
"limited",
"response",
"."
] |
df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5
|
https://github.com/sendgrid/sendgrid-go/blob/df2105ec04e32aff6b9e9a2fd1ca8e5c16a836c5/sendgrid.go#L112-L143
|
train
|
golang/tour
|
local.go
|
isRoot
|
func isRoot(path string) bool {
_, err := os.Stat(filepath.Join(path, "content", "welcome.article"))
if err == nil {
_, err = os.Stat(filepath.Join(path, "template", "index.tmpl"))
}
return err == nil
}
|
go
|
func isRoot(path string) bool {
_, err := os.Stat(filepath.Join(path, "content", "welcome.article"))
if err == nil {
_, err = os.Stat(filepath.Join(path, "template", "index.tmpl"))
}
return err == nil
}
|
[
"func",
"isRoot",
"(",
"path",
"string",
")",
"bool",
"{",
"_",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"filepath",
".",
"Join",
"(",
"path",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"_",
",",
"err",
"=",
"os",
".",
"Stat",
"(",
"filepath",
".",
"Join",
"(",
"path",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"return",
"err",
"==",
"nil",
"\n",
"}"
] |
// isRoot reports whether path is the root directory of the tour tree.
// To be the root, it must have content and template subdirectories.
|
[
"isRoot",
"reports",
"whether",
"path",
"is",
"the",
"root",
"directory",
"of",
"the",
"tour",
"tree",
".",
"To",
"be",
"the",
"root",
"it",
"must",
"have",
"content",
"and",
"template",
"subdirectories",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L50-L56
|
train
|
golang/tour
|
local.go
|
registerStatic
|
func registerStatic(root string) {
// Keep these static file handlers in sync with app.yaml.
http.Handle("/favicon.ico", http.FileServer(http.Dir(filepath.Join(root, "static", "img"))))
static := http.FileServer(http.Dir(root))
http.Handle("/content/img/", static)
http.Handle("/static/", static)
}
|
go
|
func registerStatic(root string) {
// Keep these static file handlers in sync with app.yaml.
http.Handle("/favicon.ico", http.FileServer(http.Dir(filepath.Join(root, "static", "img"))))
static := http.FileServer(http.Dir(root))
http.Handle("/content/img/", static)
http.Handle("/static/", static)
}
|
[
"func",
"registerStatic",
"(",
"root",
"string",
")",
"{",
"// Keep these static file handlers in sync with app.yaml.",
"http",
".",
"Handle",
"(",
"\"",
"\"",
",",
"http",
".",
"FileServer",
"(",
"http",
".",
"Dir",
"(",
"filepath",
".",
"Join",
"(",
"root",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
")",
")",
")",
"\n",
"static",
":=",
"http",
".",
"FileServer",
"(",
"http",
".",
"Dir",
"(",
"root",
")",
")",
"\n",
"http",
".",
"Handle",
"(",
"\"",
"\"",
",",
"static",
")",
"\n",
"http",
".",
"Handle",
"(",
"\"",
"\"",
",",
"static",
")",
"\n",
"}"
] |
// registerStatic registers handlers to serve static content
// from the directory root.
|
[
"registerStatic",
"registers",
"handlers",
"to",
"serve",
"static",
"content",
"from",
"the",
"directory",
"root",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L128-L134
|
train
|
golang/tour
|
local.go
|
rootHandler
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
if err := renderUI(w); err != nil {
log.Println(err)
}
}
|
go
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
if err := renderUI(w); err != nil {
log.Println(err)
}
}
|
[
"func",
"rootHandler",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"if",
"err",
":=",
"renderUI",
"(",
"w",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Println",
"(",
"err",
")",
"\n",
"}",
"\n",
"}"
] |
// rootHandler returns a handler for all the requests except the ones for lessons.
|
[
"rootHandler",
"returns",
"a",
"handler",
"for",
"all",
"the",
"requests",
"except",
"the",
"ones",
"for",
"lessons",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L137-L141
|
train
|
golang/tour
|
local.go
|
lessonHandler
|
func lessonHandler(w http.ResponseWriter, r *http.Request) {
lesson := strings.TrimPrefix(r.URL.Path, "/lesson/")
if err := writeLesson(lesson, w); err != nil {
if err == lessonNotFound {
http.NotFound(w, r)
} else {
log.Println(err)
}
}
}
|
go
|
func lessonHandler(w http.ResponseWriter, r *http.Request) {
lesson := strings.TrimPrefix(r.URL.Path, "/lesson/")
if err := writeLesson(lesson, w); err != nil {
if err == lessonNotFound {
http.NotFound(w, r)
} else {
log.Println(err)
}
}
}
|
[
"func",
"lessonHandler",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"lesson",
":=",
"strings",
".",
"TrimPrefix",
"(",
"r",
".",
"URL",
".",
"Path",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
":=",
"writeLesson",
"(",
"lesson",
",",
"w",
")",
";",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"lessonNotFound",
"{",
"http",
".",
"NotFound",
"(",
"w",
",",
"r",
")",
"\n",
"}",
"else",
"{",
"log",
".",
"Println",
"(",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// lessonHandler handler the HTTP requests for lessons.
|
[
"lessonHandler",
"handler",
"the",
"HTTP",
"requests",
"for",
"lessons",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L144-L153
|
train
|
golang/tour
|
local.go
|
waitServer
|
func waitServer(url string) bool {
tries := 20
for tries > 0 {
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
return true
}
time.Sleep(100 * time.Millisecond)
tries--
}
return false
}
|
go
|
func waitServer(url string) bool {
tries := 20
for tries > 0 {
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
return true
}
time.Sleep(100 * time.Millisecond)
tries--
}
return false
}
|
[
"func",
"waitServer",
"(",
"url",
"string",
")",
"bool",
"{",
"tries",
":=",
"20",
"\n",
"for",
"tries",
">",
"0",
"{",
"resp",
",",
"err",
":=",
"http",
".",
"Get",
"(",
"url",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"return",
"true",
"\n",
"}",
"\n",
"time",
".",
"Sleep",
"(",
"100",
"*",
"time",
".",
"Millisecond",
")",
"\n",
"tries",
"--",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// waitServer waits some time for the http Server to start
// serving url. The return value reports whether it starts.
|
[
"waitServer",
"waits",
"some",
"time",
"for",
"the",
"http",
"Server",
"to",
"start",
"serving",
"url",
".",
"The",
"return",
"value",
"reports",
"whether",
"it",
"starts",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L191-L203
|
train
|
golang/tour
|
local.go
|
startBrowser
|
func startBrowser(url string) bool {
// try to start the browser
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
return cmd.Start() == nil
}
|
go
|
func startBrowser(url string) bool {
// try to start the browser
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
return cmd.Start() == nil
}
|
[
"func",
"startBrowser",
"(",
"url",
"string",
")",
"bool",
"{",
"// try to start the browser",
"var",
"args",
"[",
"]",
"string",
"\n",
"switch",
"runtime",
".",
"GOOS",
"{",
"case",
"\"",
"\"",
":",
"args",
"=",
"[",
"]",
"string",
"{",
"\"",
"\"",
"}",
"\n",
"case",
"\"",
"\"",
":",
"args",
"=",
"[",
"]",
"string",
"{",
"\"",
"\"",
",",
"\"",
"\"",
",",
"\"",
"\"",
"}",
"\n",
"default",
":",
"args",
"=",
"[",
"]",
"string",
"{",
"\"",
"\"",
"}",
"\n",
"}",
"\n",
"cmd",
":=",
"exec",
".",
"Command",
"(",
"args",
"[",
"0",
"]",
",",
"append",
"(",
"args",
"[",
"1",
":",
"]",
",",
"url",
")",
"...",
")",
"\n",
"return",
"cmd",
".",
"Start",
"(",
")",
"==",
"nil",
"\n",
"}"
] |
// startBrowser tries to open the URL in a browser, and returns
// whether it succeed.
|
[
"startBrowser",
"tries",
"to",
"open",
"the",
"URL",
"in",
"a",
"browser",
"and",
"returns",
"whether",
"it",
"succeed",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/local.go#L207-L220
|
train
|
golang/tour
|
appengine.go
|
hstsHandler
|
func hstsHandler(fn http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
fn(w, r)
})
}
|
go
|
func hstsHandler(fn http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
fn(w, r)
})
}
|
[
"func",
"hstsHandler",
"(",
"fn",
"http",
".",
"HandlerFunc",
")",
"http",
".",
"Handler",
"{",
"return",
"http",
".",
"HandlerFunc",
"(",
"func",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"w",
".",
"Header",
"(",
")",
".",
"Set",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"fn",
"(",
"w",
",",
"r",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// hstsHandler wraps an http.HandlerFunc such that it sets the HSTS header.
|
[
"hstsHandler",
"wraps",
"an",
"http",
".",
"HandlerFunc",
"such",
"that",
"it",
"sets",
"the",
"HSTS",
"header",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/appengine.go#L83-L88
|
train
|
golang/tour
|
tree/tree.go
|
New
|
func New(k int) *Tree {
var t *Tree
for _, v := range rand.Perm(10) {
t = insert(t, (1+v)*k)
}
return t
}
|
go
|
func New(k int) *Tree {
var t *Tree
for _, v := range rand.Perm(10) {
t = insert(t, (1+v)*k)
}
return t
}
|
[
"func",
"New",
"(",
"k",
"int",
")",
"*",
"Tree",
"{",
"var",
"t",
"*",
"Tree",
"\n",
"for",
"_",
",",
"v",
":=",
"range",
"rand",
".",
"Perm",
"(",
"10",
")",
"{",
"t",
"=",
"insert",
"(",
"t",
",",
"(",
"1",
"+",
"v",
")",
"*",
"k",
")",
"\n",
"}",
"\n",
"return",
"t",
"\n",
"}"
] |
// New returns a new, random binary tree holding the values k, 2k, ..., 10k.
|
[
"New",
"returns",
"a",
"new",
"random",
"binary",
"tree",
"holding",
"the",
"values",
"k",
"2k",
"...",
"10k",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/tree/tree.go#L20-L26
|
train
|
golang/tour
|
solutions/fib.go
|
fibonacci
|
func fibonacci() func() int {
f, g := 1, 0
return func() int {
f, g = g, f+g
return f
}
}
|
go
|
func fibonacci() func() int {
f, g := 1, 0
return func() int {
f, g = g, f+g
return f
}
}
|
[
"func",
"fibonacci",
"(",
")",
"func",
"(",
")",
"int",
"{",
"f",
",",
"g",
":=",
"1",
",",
"0",
"\n",
"return",
"func",
"(",
")",
"int",
"{",
"f",
",",
"g",
"=",
"g",
",",
"f",
"+",
"g",
"\n",
"return",
"f",
"\n",
"}",
"\n",
"}"
] |
// fibonacci is a function that returns
// a function that returns an int.
|
[
"fibonacci",
"is",
"a",
"function",
"that",
"returns",
"a",
"function",
"that",
"returns",
"an",
"int",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/solutions/fib.go#L13-L19
|
train
|
golang/tour
|
solutions/binarytrees_quit.go
|
Walk
|
func Walk(t *tree.Tree, ch, quit chan int) {
walkImpl(t, ch, quit)
close(ch)
}
|
go
|
func Walk(t *tree.Tree, ch, quit chan int) {
walkImpl(t, ch, quit)
close(ch)
}
|
[
"func",
"Walk",
"(",
"t",
"*",
"tree",
".",
"Tree",
",",
"ch",
",",
"quit",
"chan",
"int",
")",
"{",
"walkImpl",
"(",
"t",
",",
"ch",
",",
"quit",
")",
"\n",
"close",
"(",
"ch",
")",
"\n",
"}"
] |
// Walk walks the tree t sending all values
// from the tree to the channel ch.
|
[
"Walk",
"walks",
"the",
"tree",
"t",
"sending",
"all",
"values",
"from",
"the",
"tree",
"to",
"the",
"channel",
"ch",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/solutions/binarytrees_quit.go#L31-L34
|
train
|
golang/tour
|
solutions/binarytrees_quit.go
|
Same
|
func Same(t1, t2 *tree.Tree) bool {
w1, w2 := make(chan int), make(chan int)
quit := make(chan int)
defer close(quit)
go Walk(t1, w1, quit)
go Walk(t2, w2, quit)
for {
v1, ok1 := <-w1
v2, ok2 := <-w2
if !ok1 || !ok2 {
return ok1 == ok2
}
if v1 != v2 {
return false
}
}
}
|
go
|
func Same(t1, t2 *tree.Tree) bool {
w1, w2 := make(chan int), make(chan int)
quit := make(chan int)
defer close(quit)
go Walk(t1, w1, quit)
go Walk(t2, w2, quit)
for {
v1, ok1 := <-w1
v2, ok2 := <-w2
if !ok1 || !ok2 {
return ok1 == ok2
}
if v1 != v2 {
return false
}
}
}
|
[
"func",
"Same",
"(",
"t1",
",",
"t2",
"*",
"tree",
".",
"Tree",
")",
"bool",
"{",
"w1",
",",
"w2",
":=",
"make",
"(",
"chan",
"int",
")",
",",
"make",
"(",
"chan",
"int",
")",
"\n",
"quit",
":=",
"make",
"(",
"chan",
"int",
")",
"\n",
"defer",
"close",
"(",
"quit",
")",
"\n\n",
"go",
"Walk",
"(",
"t1",
",",
"w1",
",",
"quit",
")",
"\n",
"go",
"Walk",
"(",
"t2",
",",
"w2",
",",
"quit",
")",
"\n\n",
"for",
"{",
"v1",
",",
"ok1",
":=",
"<-",
"w1",
"\n",
"v2",
",",
"ok2",
":=",
"<-",
"w2",
"\n",
"if",
"!",
"ok1",
"||",
"!",
"ok2",
"{",
"return",
"ok1",
"==",
"ok2",
"\n",
"}",
"\n",
"if",
"v1",
"!=",
"v2",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Same determines whether the trees
// t1 and t2 contain the same values.
|
[
"Same",
"determines",
"whether",
"the",
"trees",
"t1",
"and",
"t2",
"contain",
"the",
"same",
"values",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/solutions/binarytrees_quit.go#L38-L56
|
train
|
golang/tour
|
content/concurrency/mutex-counter.go
|
Inc
|
func (c *SafeCounter) Inc(key string) {
c.mux.Lock()
// Lock so only one goroutine at a time can access the map c.v.
c.v[key]++
c.mux.Unlock()
}
|
go
|
func (c *SafeCounter) Inc(key string) {
c.mux.Lock()
// Lock so only one goroutine at a time can access the map c.v.
c.v[key]++
c.mux.Unlock()
}
|
[
"func",
"(",
"c",
"*",
"SafeCounter",
")",
"Inc",
"(",
"key",
"string",
")",
"{",
"c",
".",
"mux",
".",
"Lock",
"(",
")",
"\n",
"// Lock so only one goroutine at a time can access the map c.v.",
"c",
".",
"v",
"[",
"key",
"]",
"++",
"\n",
"c",
".",
"mux",
".",
"Unlock",
"(",
")",
"\n",
"}"
] |
// Inc increments the counter for the given key.
|
[
"Inc",
"increments",
"the",
"counter",
"for",
"the",
"given",
"key",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/content/concurrency/mutex-counter.go#L18-L23
|
train
|
golang/tour
|
content/concurrency/mutex-counter.go
|
Value
|
func (c *SafeCounter) Value(key string) int {
c.mux.Lock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mux.Unlock()
return c.v[key]
}
|
go
|
func (c *SafeCounter) Value(key string) int {
c.mux.Lock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mux.Unlock()
return c.v[key]
}
|
[
"func",
"(",
"c",
"*",
"SafeCounter",
")",
"Value",
"(",
"key",
"string",
")",
"int",
"{",
"c",
".",
"mux",
".",
"Lock",
"(",
")",
"\n",
"// Lock so only one goroutine at a time can access the map c.v.",
"defer",
"c",
".",
"mux",
".",
"Unlock",
"(",
")",
"\n",
"return",
"c",
".",
"v",
"[",
"key",
"]",
"\n",
"}"
] |
// Value returns the current value of the counter for the given key.
|
[
"Value",
"returns",
"the",
"current",
"value",
"of",
"the",
"counter",
"for",
"the",
"given",
"key",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/content/concurrency/mutex-counter.go#L26-L31
|
train
|
golang/tour
|
tour.go
|
initTour
|
func initTour(root, transport string) error {
// Make sure playground is enabled before rendering.
present.PlayEnabled = true
// Set up templates.
action := filepath.Join(root, "template", "action.tmpl")
tmpl, err := present.Template().ParseFiles(action)
if err != nil {
return fmt.Errorf("parse templates: %v", err)
}
// Init lessons.
contentPath := filepath.Join(root, "content")
if err := initLessons(tmpl, contentPath); err != nil {
return fmt.Errorf("init lessons: %v", err)
}
// Init UI
index := filepath.Join(root, "template", "index.tmpl")
ui, err := template.ParseFiles(index)
if err != nil {
return fmt.Errorf("parse index.tmpl: %v", err)
}
buf := new(bytes.Buffer)
data := struct {
SocketAddr string
Transport template.JS
}{socketAddr(), template.JS(transport)}
if err := ui.Execute(buf, data); err != nil {
return fmt.Errorf("render UI: %v", err)
}
uiContent = buf.Bytes()
return initScript(root)
}
|
go
|
func initTour(root, transport string) error {
// Make sure playground is enabled before rendering.
present.PlayEnabled = true
// Set up templates.
action := filepath.Join(root, "template", "action.tmpl")
tmpl, err := present.Template().ParseFiles(action)
if err != nil {
return fmt.Errorf("parse templates: %v", err)
}
// Init lessons.
contentPath := filepath.Join(root, "content")
if err := initLessons(tmpl, contentPath); err != nil {
return fmt.Errorf("init lessons: %v", err)
}
// Init UI
index := filepath.Join(root, "template", "index.tmpl")
ui, err := template.ParseFiles(index)
if err != nil {
return fmt.Errorf("parse index.tmpl: %v", err)
}
buf := new(bytes.Buffer)
data := struct {
SocketAddr string
Transport template.JS
}{socketAddr(), template.JS(transport)}
if err := ui.Execute(buf, data); err != nil {
return fmt.Errorf("render UI: %v", err)
}
uiContent = buf.Bytes()
return initScript(root)
}
|
[
"func",
"initTour",
"(",
"root",
",",
"transport",
"string",
")",
"error",
"{",
"// Make sure playground is enabled before rendering.",
"present",
".",
"PlayEnabled",
"=",
"true",
"\n\n",
"// Set up templates.",
"action",
":=",
"filepath",
".",
"Join",
"(",
"root",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"tmpl",
",",
"err",
":=",
"present",
".",
"Template",
"(",
")",
".",
"ParseFiles",
"(",
"action",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// Init lessons.",
"contentPath",
":=",
"filepath",
".",
"Join",
"(",
"root",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
":=",
"initLessons",
"(",
"tmpl",
",",
"contentPath",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// Init UI",
"index",
":=",
"filepath",
".",
"Join",
"(",
"root",
",",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"ui",
",",
"err",
":=",
"template",
".",
"ParseFiles",
"(",
"index",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"buf",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n\n",
"data",
":=",
"struct",
"{",
"SocketAddr",
"string",
"\n",
"Transport",
"template",
".",
"JS",
"\n",
"}",
"{",
"socketAddr",
"(",
")",
",",
"template",
".",
"JS",
"(",
"transport",
")",
"}",
"\n\n",
"if",
"err",
":=",
"ui",
".",
"Execute",
"(",
"buf",
",",
"data",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"uiContent",
"=",
"buf",
".",
"Bytes",
"(",
")",
"\n\n",
"return",
"initScript",
"(",
"root",
")",
"\n",
"}"
] |
// initTour loads tour.article and the relevant HTML templates from the given
// tour root, and renders the template to the tourContent global variable.
|
[
"initTour",
"loads",
"tour",
".",
"article",
"and",
"the",
"relevant",
"HTML",
"templates",
"from",
"the",
"given",
"tour",
"root",
"and",
"renders",
"the",
"template",
"to",
"the",
"tourContent",
"global",
"variable",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/tour.go#L34-L70
|
train
|
golang/tour
|
tour.go
|
initLessons
|
func initLessons(tmpl *template.Template, content string) error {
dir, err := os.Open(content)
if err != nil {
return err
}
files, err := dir.Readdirnames(0)
if err != nil {
return err
}
for _, f := range files {
if filepath.Ext(f) != ".article" {
continue
}
content, err := parseLesson(tmpl, filepath.Join(content, f))
if err != nil {
return fmt.Errorf("parsing %v: %v", f, err)
}
name := strings.TrimSuffix(f, ".article")
lessons[name] = content
}
return nil
}
|
go
|
func initLessons(tmpl *template.Template, content string) error {
dir, err := os.Open(content)
if err != nil {
return err
}
files, err := dir.Readdirnames(0)
if err != nil {
return err
}
for _, f := range files {
if filepath.Ext(f) != ".article" {
continue
}
content, err := parseLesson(tmpl, filepath.Join(content, f))
if err != nil {
return fmt.Errorf("parsing %v: %v", f, err)
}
name := strings.TrimSuffix(f, ".article")
lessons[name] = content
}
return nil
}
|
[
"func",
"initLessons",
"(",
"tmpl",
"*",
"template",
".",
"Template",
",",
"content",
"string",
")",
"error",
"{",
"dir",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"content",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"files",
",",
"err",
":=",
"dir",
".",
"Readdirnames",
"(",
"0",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"for",
"_",
",",
"f",
":=",
"range",
"files",
"{",
"if",
"filepath",
".",
"Ext",
"(",
"f",
")",
"!=",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n",
"content",
",",
"err",
":=",
"parseLesson",
"(",
"tmpl",
",",
"filepath",
".",
"Join",
"(",
"content",
",",
"f",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"f",
",",
"err",
")",
"\n",
"}",
"\n",
"name",
":=",
"strings",
".",
"TrimSuffix",
"(",
"f",
",",
"\"",
"\"",
")",
"\n",
"lessons",
"[",
"name",
"]",
"=",
"content",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// initLessonss finds all the lessons in the passed directory, renders them,
// using the given template and saves the content in the lessons map.
|
[
"initLessonss",
"finds",
"all",
"the",
"lessons",
"in",
"the",
"passed",
"directory",
"renders",
"them",
"using",
"the",
"given",
"template",
"and",
"saves",
"the",
"content",
"in",
"the",
"lessons",
"map",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/tour.go#L74-L95
|
train
|
golang/tour
|
tour.go
|
parseLesson
|
func parseLesson(tmpl *template.Template, path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
doc, err := present.Parse(prepContent(f), path, 0)
if err != nil {
return nil, err
}
lesson := Lesson{
doc.Title,
doc.Subtitle,
make([]Page, len(doc.Sections)),
}
for i, sec := range doc.Sections {
p := &lesson.Pages[i]
w := new(bytes.Buffer)
if err := sec.Render(w, tmpl); err != nil {
return nil, fmt.Errorf("render section: %v", err)
}
p.Title = sec.Title
p.Content = w.String()
codes := findPlayCode(sec)
p.Files = make([]File, len(codes))
for i, c := range codes {
f := &p.Files[i]
f.Name = c.FileName
f.Content = string(c.Raw)
hash := sha1.Sum(c.Raw)
f.Hash = base64.StdEncoding.EncodeToString(hash[:])
}
}
w := new(bytes.Buffer)
if err := json.NewEncoder(w).Encode(lesson); err != nil {
return nil, fmt.Errorf("encode lesson: %v", err)
}
return w.Bytes(), nil
}
|
go
|
func parseLesson(tmpl *template.Template, path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
doc, err := present.Parse(prepContent(f), path, 0)
if err != nil {
return nil, err
}
lesson := Lesson{
doc.Title,
doc.Subtitle,
make([]Page, len(doc.Sections)),
}
for i, sec := range doc.Sections {
p := &lesson.Pages[i]
w := new(bytes.Buffer)
if err := sec.Render(w, tmpl); err != nil {
return nil, fmt.Errorf("render section: %v", err)
}
p.Title = sec.Title
p.Content = w.String()
codes := findPlayCode(sec)
p.Files = make([]File, len(codes))
for i, c := range codes {
f := &p.Files[i]
f.Name = c.FileName
f.Content = string(c.Raw)
hash := sha1.Sum(c.Raw)
f.Hash = base64.StdEncoding.EncodeToString(hash[:])
}
}
w := new(bytes.Buffer)
if err := json.NewEncoder(w).Encode(lesson); err != nil {
return nil, fmt.Errorf("encode lesson: %v", err)
}
return w.Bytes(), nil
}
|
[
"func",
"parseLesson",
"(",
"tmpl",
"*",
"template",
".",
"Template",
",",
"path",
"string",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n",
"doc",
",",
"err",
":=",
"present",
".",
"Parse",
"(",
"prepContent",
"(",
"f",
")",
",",
"path",
",",
"0",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"lesson",
":=",
"Lesson",
"{",
"doc",
".",
"Title",
",",
"doc",
".",
"Subtitle",
",",
"make",
"(",
"[",
"]",
"Page",
",",
"len",
"(",
"doc",
".",
"Sections",
")",
")",
",",
"}",
"\n\n",
"for",
"i",
",",
"sec",
":=",
"range",
"doc",
".",
"Sections",
"{",
"p",
":=",
"&",
"lesson",
".",
"Pages",
"[",
"i",
"]",
"\n",
"w",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"if",
"err",
":=",
"sec",
".",
"Render",
"(",
"w",
",",
"tmpl",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"p",
".",
"Title",
"=",
"sec",
".",
"Title",
"\n",
"p",
".",
"Content",
"=",
"w",
".",
"String",
"(",
")",
"\n",
"codes",
":=",
"findPlayCode",
"(",
"sec",
")",
"\n",
"p",
".",
"Files",
"=",
"make",
"(",
"[",
"]",
"File",
",",
"len",
"(",
"codes",
")",
")",
"\n",
"for",
"i",
",",
"c",
":=",
"range",
"codes",
"{",
"f",
":=",
"&",
"p",
".",
"Files",
"[",
"i",
"]",
"\n",
"f",
".",
"Name",
"=",
"c",
".",
"FileName",
"\n",
"f",
".",
"Content",
"=",
"string",
"(",
"c",
".",
"Raw",
")",
"\n",
"hash",
":=",
"sha1",
".",
"Sum",
"(",
"c",
".",
"Raw",
")",
"\n",
"f",
".",
"Hash",
"=",
"base64",
".",
"StdEncoding",
".",
"EncodeToString",
"(",
"hash",
"[",
":",
"]",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"w",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n",
"if",
"err",
":=",
"json",
".",
"NewEncoder",
"(",
"w",
")",
".",
"Encode",
"(",
"lesson",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"return",
"w",
".",
"Bytes",
"(",
")",
",",
"nil",
"\n",
"}"
] |
// parseLesson parses and returns a lesson content given its name and
// the template to render it.
|
[
"parseLesson",
"parses",
"and",
"returns",
"a",
"lesson",
"content",
"given",
"its",
"name",
"and",
"the",
"template",
"to",
"render",
"it",
"."
] |
db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77
|
https://github.com/golang/tour/blob/db40fe78fefcfdeefb4ac4a94c72cfb20f40ee77/tour.go#L120-L161
|
train
|
Subsets and Splits
SQL Console for semeru/code-text-go
Retrieves a limited set of code samples with their languages, with a specific case adjustment for 'Go' language.