Golang functions: Difference between revisions
From wikinotes
(Created page with "<syntaxhighlight lang="go"> func greet(name string) string { return "Hello, " + name } </syntaxhighlight>") |
No edit summary |
||
Line 1: | Line 1: | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
// (param type) (return-type) | |||
func greet(name string) string { | func greet(name string) string { | ||
return "Hello, " + name | return "Hello, " + name | ||
} | |||
</syntaxhighlight> | |||
Multiple return values | |||
<syntaxhighlight lang="go"> | |||
// when multiple return values present, surround with brackets | |||
func find(id int) (string, int) { | |||
// ... | |||
} | |||
// optionally, return values can be named | |||
// (this has no implications for caller, it's simply documentation) | |||
func find(id int) (name string, age int) { | |||
// ... | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 20:55, 23 May 2022
// (param type) (return-type)
func greet(name string) string {
return "Hello, " + name
}
Multiple return values
// when multiple return values present, surround with brackets
func find(id int) (string, int) {
// ...
}
// optionally, return values can be named
// (this has no implications for caller, it's simply documentation)
func find(id int) (name string, age int) {
// ...
}