fmt.Println()
We can also define our own functions.func
keyword. We have seen func
keyword before in our code with main
. main()
is a special function which is required for every executable Go code.func
keyword followed by name of function. A function need to have a body, which defines code statements to execute when called.sayHello
is a function which will print Hello
when called.()
sayHello.
func func_name(parameter_list) { func_body }
(paramter_name_1 paramter_type, parameter_name_2 parameter_type)
message
is the name of parameter and it will store string
values passed to it.sayHello("Hello, World!")
func func_name(parameter_list) (return_list) { func_body }
(return_type1, return_type2, return_type3)
sayHello
returns a string.
We have defined result
as type of string
as local variable for our main
function. When sayHello
returns string
, it is being stored in result variable.readFile
function return 2 values a string
and an error
. While calling this function, we need to provide 2 variables which will capture this return values. We have used result
and err
variable to store these values.