2. Inputs and results
Each named function declares its typed inputs immediately after its Define sentence. Today the compiler supports integer and integer sequence inputs.
Complete program
Define filter-and-double as a function.
It accepts an integer sequence named input and an integer named threshold.
Collect the input values in encounter order into a fresh sequence. Retain each value only when it is greater than the threshold. Replace each retained value with the doubled value of that value before appending it.Every input is mandatory and keeps authored binding order. another integer introduces another typed input; it does not imply equality or inequality.
Generated Go:
package generated
import (
"errors"
"math"
)
func FilterAndDouble(p0 []int64, p1 int64) ([]int64, error) {
result := make([]int64, 0)
for _, value := range p0 {
if value > p1 {
if value > math.MaxInt64/2 || value < math.MinInt64/2 {
return nil, errors.New("integer overflow")
}
result = append(result, value+value)
}
}
return result, nil
}The emitted function returns a []int64 and an error because doubling uses checked arithmetic. Inputs remain read-only.
Names use ASCII letters and internal hyphens. A Stated function name such as filter-and-double becomes the exported Go name FilterAndDouble.
Exercise
Change the function name and compile again. Where does that name appear in the Go?
Verified solution
The name from Define … as a function. is converted to an exported Go identifier and used for the emitted function declaration.