Summary: in this tutorial, you will learn how to define Go functions to modulize your code and make it more reusable.
Functions are basic building blocks in Go. Functions allow you to encapsulate reusable logic, making your code more reusable.
Defining a function
To define a function, you use the func
keyword, followed by the function name, parameters (optional), and the return type (if any):
func name(parameters) type {
// body
}
Code language: Go (go)
For example, the following defines a function sayHi
:
func sayHi() {
fmt.Println("Hi")
}
Code language: Go (go)
Calling a function
To call a function, you use the function name followed by the parentheses. For example, the following illustrates how to call the sayHi()
function inside the main()
function:
package main
import "fmt"
func sayHi() {
fmt.Println("Hi")
}
func main() {
sayHi()
}
Code language: Go (go)
Output:
Hi
Code language: Go (go)
Function parameters
Functions may accept one or more parameters, allowing you to pass values to them. To define parameters for a function, you specify them within the parentheses following the function name.
For example, the following modifies the sayHi()
function that accepts a string parameter:
func sayHi(name string) {
fmt.Println("Hi", name)
}
Code language: Go (go)
When calling the sayHi()
function, you need to pass a value to the name
parameter:
package main
import "fmt"
func sayHi(name string) {
fmt.Println("Hi", name)
}
func main() {
sayHi("Joe")
}
Code language: Go (go)
Output:
Hi Joe
Code language: Go (go)
The string "Joe"
is called an argument of the sayHi()
function.
Parameters are defined in the function declaration while arguments are values you pass to the function.
Return values
A function may return a value. In this case, you need to specify the function’s return type after the parameter list.
For example, the following defines the sayHi()
function that returns a string:
func sayHi(name string) string {
return "Hi " + name
}
Code language: Go (go)
Here’s the complete code:
package main
import "fmt"
func sayHi(name string) string {
return "Hi " + name
}
func main() {
fmt.Println(sayHi("Joe"))
}
Code language: Go (go)
Output:
Hi Joe
Named Return Value
Go allows you to assign a name to the return value. For example:
func sayHi(name string) (message string) {
message = "Hi " + name
return
}
Code language: Go (go)
In this example, we assign the message
as the name of the return value of the sayHi
function. Go treats the message
as a variable defined at the top of the sayHi
function.
Typically, you use the named return value to document the meaning of the return value.
A return
statement that does not have any argument will return the named return value. It is known as a “naked” return:
return
Code language: Go (go)
In practice, you should use the naked return value in short functions only since it will make the code less readable.
Variadic Functions
A function can accept a variable number of arguments using the ...
syntax. It means a function may accept one parameter, two parameters, and so on. This function is called a variadic function. For example:
package main
import "fmt"
func sayHi(names ...string) {
for _, name := range names {
fmt.Println("Hi",name )
}
}
func main() {
sayHi("Joe","Jane", "Bob");
}
Code language: Go (go)
Output:
Hi Joe
Hi Jane
Hi Bob
Code language: Go (go)
In this example, the sayHi function has a variable number of string parameters. Inside the function, we iterate over each parameter using a for range loop and display the corresponding greeting message.
Function Literal
In Go, you can define functions without names. These functions are called functional literals. In other programming languages such as JavaScript, they are known as anonymous functions.
For example, the following shows how to define a function literal function and call it immediately:
package main
import "fmt"
func main() {
func(names ...string){
for _, name := range names {
fmt.Println("Hi",name )
}
}("Joe","Jane", "Bob")
}
Code language: Go (go)
In this example:
First, define the function literal that accepts zero or more string parameters:
func(names ...string){
for _, name := range names {
fmt.Println("Hi",name )
}
}
Code language: Go (go)
Second, immediately call the functional literal and pass three string arguments:
func(names ...string){
for _, name := range names {
fmt.Println("Hi",name )
}
}("Joe","Jane", "Bob")
Code language: Go (go)
In practice, you often use function literals as inline functions.
Summary
- Use functions to encapsulate reusable logic.
- Use the
func
keyword to define a function. - A function can accept parameters and return values.
- A variadic function can accept a variable number of arguments.
- A functional literal is a function without a name.