Call a function: Difference between revisions

Content added Content deleted
Line 1,875: Line 1,875:
i = len(list)</lang>
i = len(list)</lang>
* Go has no subroutines, just functions and methods.
* 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.
* Go arguments are passed by value.
::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 or by reference
<lang go>package main
<lang go>package main


Line 1,900: Line 1,899:
fmt.Println("pointer:", &i) // prt pointer: 0xc0000140b8
fmt.Println("pointer:", &i) // prt pointer: 0xc0000140b8
}</lang>
}</lang>
* Partial application is not directly supported.
* Partial and Currying is not directly supported.
<lang go>package main

import "fmt"

func mkAdd(a int) func(int) int {
return func(b int) int {
return a + b
}
}
func sum(x, y int) int {
return x + y
}

func partialSum(x int) func(int) int {
return func(y int) int {
return sum(x, y)
}
}
func main() {
// Is partial application possible and how
add2 := mkAdd(2)
add3 := mkAdd(3)
fmt.Println(add2(5), add3(6)) // prt 7 9
// Currying functions in go
partial := partialSum(13)
fmt.Println(partial(5)) //prt 18
}</lang>
::However something similar can be done, see [[Partial function application#Go]]
::However something similar can be done, see [[Partial function application#Go]]