In Go, creating a function with a parameter is as simple as defining the function with the variable name and type in parentheses after the function name. Here's an example:
main.go48 chars4 lines
In this example, the function add
takes two integer parameters (x
and y
) and returns their sum as an integer.
You can also define multiple parameters with the same type by separating them with commas:
main.go98 chars4 lines
In this example, the greet
function takes two parameters of type string
(firstName
and lastName
) and prints a greeting to the console.
Now, you can call these functions and pass values to the parameters:
main.go79 chars3 lines
gistlibby LogSnag