Call a function: Difference between revisions

Line 1,823:
* Optional arguments and named arguments are not supported.
::However, it is reasonably common to see a structure used for this. In this example <code>gif.Options</code> is a structure with multiple members which can initialized/assigned by name or omitted (or the whole third argument can just be <code>nil</code>). <lang go> gif.Encode(ioutil.Discard, image.Black, &gif.Options{NumColors: 16})</lang>
* Optional arguments are supported.
<lang go>
package main
 
import "fmt"
 
type Params struct {
a, b, c int
}
 
func doIt(p Params) int {
return p.a + p.b + p.c
}
func main() {
fmt.Println(doIt(Params{a: 1, c: 9})) // prt 10
}
</lang>
* Named arguments are supported.
<lang go>
package main
 
import "fmt"
 
func bar(a, b, c int) {
fmt.Printf("%d, %d, %d", a, b, c)
}
func main() {
 
args := make(map[string]int)
args["a"] = 3
args["b"] = 2
args["c"] = 1
bar(args["a"], args["b"], args["c"]) // prt 3, 2, 1
}
</lang>
* Within a statement context.
::Assignment statements are shown later. Only functions returning a single value can be used in a single value context: <lang go> if 2*g(1, 3.0)+4 > 0 {}</lang>
Anonymous user