This is the sixth entry of my weekly series Learning Go. Last week I covered the Struct and Interface types. This week I will be talking about Function Declarations, Arguments, Parameters, and Anonymous Functions.
The role that functions play in Go, or in any programming language, is the same. They exist to perform computation, data fetching, aggregation, and many other utilities. A few notes on functions before we jump into a few examples:
There are several ways to create a function. The first one I will cover, is probably the most traditional; however, it is not exclusive to how we declare functions.
Before we jump into an example, let me break down the four pieces of a Function Declaration:
Quick note: a receiver is not required. These are used in receiver functions which we will cover later. Another thing to keep in mind, parameters and return types are optional.
If your function does not expect to get any values when it is called, you can leave the parameters empty.
Likewise, if your function does not return a value, you do not need a return type.
Let’s look at a basic example of a function declaration, first, without a receiver type, parameters, or a return type.
package main
import (
"fmt"
)
func main() {
sayHello()
// hello!
}
func sayHello() {
fmt.Println("hello!")
}
func main we declare a new function using the func keywordsayHello() is where your parameters go. We do not have any in this example; however, you still need to have themmain simply by writing the function identifier with a set of parentheses ()Why do we need parentheses immediately following our identifier? This is how you would pass values into your function. These values are called arguments. If we do not have any values to pass, we still have to write a set of parentheses, this lets the compiler know that we want to execute this function and that it has no arguments.
Let’s create a function that uses arguments and parameters.
package main
import (
"fmt"
)
func main() {
myName("martin")
// hello martin
}
func myName(s string) {
fmt.Println("hello", s)
}
func keyword with the identifier myNamemyName, you will see we put s string between parenthesess is the value we will receive from this function and string tells us that s will be of type stringmainwe write our function identifier, myName"martin" of type string inside of the parentheses, this is the function’s argumentmyName is then executed and it prints "hello martin"We have seen a few very basic ideas of the role that functions can play in your programs; however, I am confident that you will not use functions just to print values. Let’s see an example of a function that returns a value:
package main
import (
"fmt"
)
func main() {
n := sayHello("martin")
fmt.Println(n)
// hello from martin
}
func sayHello(s string) string {
return fmt.Sprint("hello from ", s)
}
From a code organization standpoint, there will come a time that you need to assign a variable to the returned value of a function in order to do something else useful with it.
func main, we declare a function using the func keywordsayHellos of type string between parenthesesstringreturn keyword, we return the value of s from this functionfunc main we declare a new variable n that is equal to the returned value of the sayHello functionnIn Go, it is possible to have more than one value returned from a function. Let’s see how that works in an example below:
package main
import (
"fmt"
)
func main() {
x, y := isAJedi("obi wan", "kenobi")
fmt.Println(x, y)
// obi wan kenobi true
}
func isAJedi(s1, s2 string) (string, bool) {
a := fmt.Sprint(s1, " ", s2)
b := true
return a, b
}
func keywordisAJedis1 and s2, both of type stringstring and boola and assign it to the value of a string that includes the values of s1 and s2b and assign it to the value true of type boolreturn keyword we write the variables a and b
string and bool, order matters; therefore, we can not return a bool value and then a string valuefunc main we declare x and y as variables that will be assigned the value of each returned value from isAJedix is obi wan kenobi and the value of y is trueAs we have already seen, there are many ways you can create and use a function. An anonymous function is used for when you don’t need a function with an identifier:
package main
import (
"fmt"
)
func main() {
func() {
fmt.Println("I'm anonymous!")
}()
// I'm anonymous!
}
func main we use the func keyword with(){fmt package, we print the string I'm anonymous!} on the next line} you will notice we have a set of empty parentheses (), as mentioned previously, this is how we tell the compiler to execute this function()I hope you have enjoyed learning about Function Declarations, Arguments, Parameters, and Anonymous Functions. There is so much more to learn about functions in Go, and I am excited to share more with you in the coming weeks. Next week we will dive into Function Expressions and Closure. Can’t wait, see you then!