Call a function: Difference between revisions

Line 1,875:
i = len(list)</lang>
* Go has no subroutines, just functions and methods.
* Go arguments are passed by value. or by reference
 
::As with C, a pointer can be used to achieve the effect of reference passing. (Like pointers, slice arguments have their contents passed by reference, it's the slice header that is passed by value).
* Go arguments are passed by value.
<lang go>package main
 
import "fmt"
 
// int parameter, so arguments will be passed to it by value.
func zeroval(ival int) {
ival = 0
}
// has an *int parameter, meaning that it takes an int pointer.
func zeroptr(iptr *int) {
*iptr = 0
}
func main() {
i := 1
fmt.Println("initial:", i) // prt initial: 1
zeroval(i)
fmt.Println("zeroval:", i) // prt zeroval: 1
zeroptr(&i)
fmt.Println("zeroptr:", i) // prt zeroptr: 0
fmt.Println("pointer:", &i) // prt pointer: 0xc0000140b8
}</lang>
* Partial application is not directly supported.
::However something similar can be done, see [[Partial function application#Go]]
Anonymous user